马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
Example:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,4,7,5,3,6,8,9]
Explanation:
Note:
The total number of elements of the given matrix will not exceed 10,000.
class Solution:
def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]:
result = []
if matrix == None or len(matrix) == 0 or len(matrix[0]) == 0:
return result
m = len(matrix)
n = len(matrix[0])
hashmap = collections.defaultdict(list)
for i in range(m):
for j in range(n):
hashmap[i + j].append(matrix[i][j])
for k in sorted(hashmap.keys()):
temp = hashmap[k]
if k % 2 == 0:
temp = temp[::-1]
result += temp
return result
|