Skip to content

Commit

Permalink
498. Diagonal Traverse
Browse files Browse the repository at this point in the history
498. Diagonal Traverse solved
  • Loading branch information
hpmsora committed Feb 15, 2024
1 parent 9bbc593 commit a2d4f4e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
|0455|[Arithmetic Slices II - Subsequence](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/) | [Python3](/Solutions/0446_Arithmetic_Slices_II_Subsequence.py) | Hard |
|0455|[Assign Cookies](https://leetcode.com/problems/assign-cookies/) | [Python3](/Solutions/0455_Assign_Cookies.py) | Easy |
|0459|[Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/) | [Python3](/Solutions/0459_Repeated_Substring_Pattern.py) | Easy |
|0498|[Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/) | [Python3](/Solutions/0498_Diagonal_Traverse.py) | Medium |
|0501|[Repeated Substring Pattern](https://leetcode.com/problems/find-mode-in-binary-search-tree/) | [Python3](/Solutions/0501_Find_Mode_in_Binary_Search_Tree.py) | Easy |
|0515|[Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) | [Python3](/Solutions/0515_Find_Largest_Value_in_Each_Tree_Row.py) | Medium |
|0523|[Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/) | [Python3](/Solutions/0523_Continuous_Subarray_Sum.py) | Medium |
Expand Down
38 changes: 38 additions & 0 deletions Solutions/0498_Diagonal_Traverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
n_row = len(mat) # num of row
n_col = len(mat[0]) # num of column

# List reserve List[List[int]]
sol_list = []

# start with top row
for row in range(n_row):
temp_list = []
for col in range(n_col):
if row < 0:
break
temp_list = [mat[row][col]] + temp_list
row -= 1
sol_list.append(temp_list)

# start with right column
for col in range(1, n_col):
temp_list = []
for row in range(n_row):
if col >= n_col:
break
temp_list = [mat[n_row - row - 1][col]] + temp_list
col += 1
sol_list.append(temp_list)

# organize sol_list
sol = []
isReversed = True
for each_sol_list in sol_list:
if isReversed:
sol += each_sol_list[::-1]
else:
sol += each_sol_list
isReversed = isReversed == False
return sol

0 comments on commit a2d4f4e

Please sign in to comment.