Guest User

Untitled

a guest
Jun 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. class Solution:
  2. def findDiagonalOrder(self, matrix):
  3. """
  4. :type matrix: List[List[int]]
  5. :rtype: List[int]
  6. """
  7. if not matrix: return []
  8. i, j, k = 0, 0, 1
  9. w, h = len(matrix), len(matrix[0])
  10. ans = []
  11. for x in range(w * h):
  12. ans.append(matrix[i][j])
  13. if k > 0:
  14. di, dj = i - 1, j + 1
  15. else:
  16. di, dj = i + 1, j - 1
  17. if 0 <= di < w and 0 <= dj < h:
  18. i, j = di, dj
  19. else:
  20. if k > 0:
  21. if j + 1 < h:
  22. j += 1
  23. else:
  24. i += 1
  25. else:
  26. if i + 1 < w:
  27. i += 1
  28. else:
  29. j += 1
  30. k *= -1
  31. return ans
Add Comment
Please, Sign In to add comment