题解 | #螺旋矩阵#

螺旋矩阵

https://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param matrix int整型二维数组 
# @return int整型一维数组
# 简单的模拟
class Solution:
    def spiralOrder(self , matrix: List[List[int]]) -> List[int]:
        if matrix is None:
            return None
        res = []
        if len(matrix) == 0 or len(matrix[0]) == 0:
            return res
        else:
            count = len(matrix) * len(matrix[0])
        up = 0
        down = len(matrix)-1
        right = len(matrix[0])-1
        left = 0
        i = 0
        print(matrix)
        print(count)
        
        while count: # and left <= right and up <= down:
            while left <= i  <= right:
                #if up < len(matrix) and i < len(matrix[0]):
                #    res.append(matrix[up][i])
                res.append(matrix[up][i])
                i += 1
            up += 1
            if up > down:
                break
            i = up
            while up <= i and i <= down:
                res.append(matrix[i][right])
                i += 1
            right -= 1
            if left > right:
                break
            i = right
            while left <= i:
                res.append(matrix[down][i])
                i -= 1
            down -= 1
            if up > down:
                break
            i = down
            while up <= i:
                res.append(matrix[i][left])
                i -= 1
            left += 1
            if left > right:
                break
            i = left
            count -= 1
        return res

全部评论

相关推荐

10-20 15:26
门头沟学院 Java
桥头牛油火锅:这个比例不正常,简历的话项目经历放中间,项目功能分点可以再明确点,前面加“·”或者“1 2 3”,另外简历上的照片可以去外面摄影店拍一下,以后也会用到的,hr筛人也是多少会看的,毕竟世界是一个巨大的卡颜局嘛,还有有些hr由于消息太多可能没看到,后面可能会回来找你,要简历的还会多一点,我也是普2本,比例大致是600:90:15:3,当然我实力不太够,拿的offer比较少,慢慢来吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务