Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Medium? Seriously? 

Well again, always smart solutions out there: https://discuss.leetcode.com/topic/77889/3-line-python-solution

class Solution(object):
    def findDiagonalOrder(self, matrix):
        r = []
        n = len(matrix)
        if n == 0: return r
        m = len(matrix[0])
        
        ni = n + m - 1
        for i in range(ni):
            rr = []
            x = min(n - 1, i)
            y = max(i - n + 1, 0)
            while x >= 0 and y < m:
                rr += [matrix[x][y]]
                x = x - 1
                y = y + 1
            if i % 2 == 1:
                rr.reverse()
            r = r + rr 
 
        return r
        
posted on 2017-02-10 05:07  Tonix  阅读(192)  评论(0编辑  收藏  举报