Seawolf 发表于 2020-8-2 07:19:46

Leetcode 498. Diagonal Traverse

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:

Explanation:



Note:

The total number of elements of the given matrix will not exceed 10,000.

class Solution:
    def findDiagonalOrder(self, matrix: List]) -> List:
      result = []
      if matrix == None or len(matrix) == 0 or len(matrix) == 0:
            return result
      m = len(matrix)
      n = len(matrix)
      hashmap = collections.defaultdict(list)
      for i in range(m):
            for j in range(n):
                hashmap.append(matrix)
      for k in sorted(hashmap.keys()):
            temp = hashmap
            if k % 2 == 0:
                temp = temp[::-1]
            result += temp
      return result
页: [1]
查看完整版本: Leetcode 498. Diagonal Traverse