您当前的位置: 首页 >  矩阵

孑渡

暂无认证

  • 7浏览

    0关注

    178博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Leetcode】剑指Offer 29:顺时针打印矩阵

孑渡 发布时间:2022-09-19 09:51:23 ,浏览量:7

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。 示例 1: 输入:matrix = [[1,2,3],[4,5,6],[7,8,9]] 输出:[1,2,3,6,9,8,7,4,5] 示例 2: 输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] 输出:[1,2,3,4,8,12,11,10,9,5,6,7] 限制: 0 List[int]: if not matrix or not matrix[0]: return list() rows, columns = len(matrix), len(matrix[0]) visited = [[False] * columns for _ in range(rows)] total = rows * columns order = [0] * total directions = [[0, 1], [1, 0], [0, -1], [-1, 0]] row, column = 0, 0 directionIndex = 0 for i in range(total): order[i] = matrix[row][column] visited[row][column] = True nextRow, nextColumn = row + directions[directionIndex][0], column + directions[directionIndex][1] if not (0

关注
打赏
1663211900
查看更多评论
立即登录/注册

微信扫码登录

0.0373s