Not efficient but no diagonal logic - just traverse


  • 0
    P
    class Solution(object):
        def findDiagonalOrder(self, matrix):
            """
            :type matrix: List[List[int]]
            :rtype: List[int]
            """
            if not matrix:
                return []
            rows = len(matrix)
            cols = len(matrix[0])
            
            prod = rows*cols 
            ans = [] 
            
            r = 0
            j = 0
            
            count = 0
            while count<prod:
                while r>=0 and j<cols and count<prod:
                    count+=1
                    ans.append(matrix[r][j])
                    r-=1
                    j+=1
        
                r+=1
                if j==cols:
                    j-=1
                    r+=1
        
                while r<rows and j>=0 and count<prod:
                    count+=1
                    ans.append(matrix[r][j])
                    r+=1
                    j-=1
        
                j+=1
                if r==rows:
                    r-=1
                    j+=1
            
            return ans 
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.