All elements on the same diagonal have same (row + col) values. Easy-to-understand Python solution


  • 1
    W

    '''

    from collections import deque
    class Solution(object):
    
        def findDiagonalOrder(self, matrix):
        
            if not matrix or not matrix[0]:
                return []
        
            diagonalrow = len(matrix) + len(matrix[0]) + 1
            flatten = [deque() for i in xrange(diagonalrow)]
        
            for i in xrange(len(matrix)):
                for j in xrange(len(matrix[0])):
                    index = i + j
                    if index % 2 == 0:
                        flatten[index].appendleft(matrix[i][j])
                    else:
                        flatten[index].append(matrix[i][j])
        
            ans = []
            for seq in flatten:
                ans.extend(seq)
            
            return ans
    

    '''


Log in to reply
 

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