Python EZ understanding approach


  • 0

    Given start and end point, function P will get all the elements of the line.

    So core objective is to find all the start and end. Luckily, they should be easy to get and just look at how are ly, lx, ry, rx are culculated from the code.

    class Solution(object):
        def findDiagonalOrder(self, matrix):
            if not matrix:
                return []
            h, w = len(matrix), len(matrix[0])
            ans = []
            
            def P(a, b, dir):
                ans.append(matrix[a[0]][a[1]])
                while a != b:
                    a[0] -= dir
                    a[1] += dir
                    ans.append(matrix[a[0]][a[1]])
                    
            for i in range(w + h - 1):
                ly, lx = i if i < h else h - 1, 0 if i < h else i - h + 1
                ry, rx = 0 if i < w else i - w + 1, i if i < w else w - 1
                if i % 2 == 0:
                    P([ly, lx], [ry, rx], 1)
                else:
                    P([ry, rx], [ly, lx], -1)
            return ans
                
    

Log in to reply
 

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