-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagonalTraverse.py
50 lines (45 loc) · 1.4 KB
/
diagonalTraverse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#https://leetcode.com/explore/learn/card/array-and-string/202/introduction-to-2d-array/1167/
import collections
class Solution(object):
def findDiagonalOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,4,7,5,3,6,8,9]
explanation:
[a11, a12, a13],
[a21, a22, a23],
[a31, a32, a33]
sum of the indices along the required diagonal order are all equal, see below:
1+2 = 2+1
1+3 = 2+2 = 3+1
2+3 = 3+2
3+3
output = [1,2,4,7,5,3,6,8,9]
i = 0,1,2,3,4,5,6,7,8
use dictionary to maintain the odd and even indices sum
"""
dic = collections.defaultdict(list)
result = []
for i in range(0, len(matrix)):
for j in range(0, len(matrix[0])):
print(matrix[i][j])
dic[i+j+1].append(matrix[i][j])
for i in dic.keys():
if i%2 == 1:
dic[i].reverse()
result+=dic[i]
return result
s = Solution()
matrix = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
s.findDiagonalOrder(matrix)