Diagonal Traverse | diagonal traverse | leetcode 498 | matrix

preview_player
Показать описание


Popular Playlists:



#Matrix #Diagonal #Array #December #Leetcode #Medium #Algorithm #DataStructure #Java #Preparation #NG #nickode #CookCodeTravel #CCT
Рекомендации по теме
Комментарии
Автор

Thanks for the video. Forgot to mention that the ordering of checking is very important.

SameerSrinivas
Автор

Thank you, you made it easy, , , the below is my solution
class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
rows_length = len(mat)
cols_length = len(mat[0])
result = []
row, col = 0, 0
if rows_length ==0 or cols_length ==0:
return []
for i in range(rows_length * cols_length):
result.append(mat[row][col])
if (row+col)%2==0:
#will go up
if col == cols_length-1:
row +=1
elif row ==0:
col +=1
else:
row -=1
col +=1
else:
#will go down
if row == rows_length-1:
col +=1
elif col ==0:
row +=1
else:
row +=1
col -=1
return result

aryansathish
Автор

Thanks for the solution and clear explanation. Why (r+c)%2 to decide the direction?

sriracha
Автор

By using Unordered_map, Answer is not comming and while using Map ans is comming in C++ why so?

anupdogrial
welcome to shbcf.ru