Advertisement
smj007

Untitled

Mar 16th, 2025
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. class Solution:
  2.     def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]:
  3.         """
  4.        0: [0, 0]
  5.        1: [0, 1] [1, 0]
  6.        2: [2, 0] [1, 1] [0, 2]
  7.        3: [1, 2] [2, 1]
  8.        4: [2, 2]
  9.        """
  10.  
  11.         m, n = len(matrix), len(matrix[0])
  12.  
  13.         hashmap = defaultdict(list)
  14.         result = []
  15.  
  16.         for i in range(m):
  17.             for j in range(n):
  18.                 hashmap[(i+j)].append(matrix[i][j])
  19.  
  20.         for i in range(m-1 + n-1 + 1):
  21.             if i%2 == 0:
  22.                 result.extend(hashmap[i][::-1])
  23.             else:
  24.                 result.extend(hashmap[i])
  25.  
  26.         return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement