码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: 1424. Diagonal Traverse II

时间:2021-03-09 12:53:59      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:return   continue   import   script   level   order   range   pen   ==   

Description

code

这是考验智商的题目~~


我的代码

import bisect

class Solution(object):
    def findDiagonalOrder(self, nums):
        """
        :type nums: List[List[int]]
        :rtype: List[int]
        """
        
        ret = []
        N = len(nums)
        rows = [i for i in range(N)]
        
        def iter1(level):
            pos = bisect.bisect_left(rows, level)
            
            if level == N or pos >= len(rows) or rows[pos] != level:
                return
                      
            for j in reversed(rows[:pos+1]):
                if j > level:
                    continue
                p = level-j    
                if len(nums[j]) > p:
                    ret.append(nums[j][p])
                else:
                    rows.remove(j)                
            return iter1(level+1)
        
        def iter2(level, pos):
            if 0 > level or level not in rows:
                return
            if pos >= len(nums[level]):
                rows.remove(level)
                if len(rows) == 0:
                    return
                return iter2(rows[-1], pos+level-rows[-1])
            
            
            for j in reversed(rows):
                p = level-j+pos
                if len(nums[j]) > p:
                    ret.append(nums[j][p])
                else:
                    rows.remove(j)
                
            return iter2(level, pos+1)
        
        iter1(0)
        iter2(N-1, 1)
        return ret
          

lee215 代码
     def findDiagonalOrder(self, A):
        res = []
        for i, r in enumerate(A):
            for j, a in enumerate(r):
                if len(res) <= i + j:
                    res.append([])
                res[i + j].append(a)
        return [a for r in res for a in reversed(r)]

性能、简洁度都被吊打~

Leetcode: 1424. Diagonal Traverse II

标签:return   continue   import   script   level   order   range   pen   ==   

原文地址:https://www.cnblogs.com/tmortred/p/14496830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!