LeetCode算法题解
  • 关于
  • 简单

    • 1. 两数之和
    • 78. 子集
    • 141. 环形链表
    • 237. 删除链表中的节点
    • 590. N叉树的后序遍历
    • 746. 使用最小花费爬楼梯
    • 938. 二叉搜索树的范围和
    • 1025. 除数博弈
    • 1108. IP 地址无效化
    • 1221. 分割平衡字符串
    • 1281. 整数的各位积和之差
    • 1290. 二进制链表转整数
    • 1295. 统计位数为偶数的数字
    • 1431. 拥有最多糖果的孩子
    • LCP 1.猜数字
    • 面试题 17.16. 按摩师
    • 面试题53 - II. 0~n-1中缺失的数字
  • 中等

    • 3. 无重复字符的最长子串
    • 6. Z 字形变换
    • 11. 盛最多水的容器
    • 15. 三数之和
    • 17. 电话号码的字母组合
    • 22. 括号生成
    • 24. 两两交换链表中的节点
    • 39. 组合总和
    • 46. 全排列
    • 48. 旋转图像
    • 54. 螺旋矩阵
    • 55. 跳跃游戏
    • 59. 螺旋矩阵 II
    • 77. 组合
    • 94. 二叉树的中序遍历
    • 109. 有序链表转换二叉搜索树
    • 114. 二叉树展开为链表
    • 147. 对链表进行插入排序
    • 207. 课程表
    • 208. 实现 Trie (前缀树)
    • 236. 二叉树的最近公共祖先
    • 238. 除自身以外数组的乘积
    • 260. 只出现一次的数字 III
    • 319. 灯泡开关
    • 338. 比特位计数
    • 400. 第N个数字
    • 429. N叉树的层序遍历
    • 513. 找树左下角的值
    • 535.TinyURL 的加密与解密
    • 537. 复数乘法
    • 547. 朋友圈
    • 654. 最大二叉树
    • 701. 二叉搜索树中的插入操作
    • 739. 每日温度
    • 797. 所有可能的路径
    • 807. 保持城市天际线
    • 814. 二叉树剪枝
    • 877. 石子游戏
    • 921. 使括号有效的最少添加
    • 946. 验证栈序列
    • 950. 按递增顺序显示卡牌
    • 1008. 先序遍历构造二叉树
    • 1014. 最佳观光组合
    • 1161. 最大层内元素和
    • 1227. 飞机座位分配概率
    • 1282. 用户分组
    • 1305. 两棵二叉搜索树中的所有元素
    • 1315. 祖父节点值为偶数的节点和
    • 5153. 层数最深叶子节点的和
    • 面试题 16.24. 数对和
    • 面试题46. 把数字翻译成字符串
  • 困难

    • 4. 寻找两个正序数组的中位数
    • 51. N皇后
    • 57. 插入区间
    • 145. 二叉树的后序遍历
    • 239. 滑动窗口最大值
    • 297. 二叉树的序列化与反序列化
    • 980. 不同路径 III
    • 1172. 餐盘栈

题目描述

给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

示例 1:

输入:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]
输入:
[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]
]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]

思路

用一个游标按题意遍历二维数组即可。

解法

/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
const spiralOrder = matrix => {
  const result = [];

  const boundary = {
    left: 0,
    top: 0,
    bottom: matrix.length - 1,
    right: (matrix[0] || 0).length - 1,
  };

  let direction = 'right';
  let cursor = [0, 0];

  const walk = direction => {
    switch (direction) {
      case 'right':
        cursor[1] += 1;
        break;
      case 'bottom':
        cursor[0] += 1;
        break;
      case 'left':
        cursor[1] -= 1;
        break;
      case 'top':
        cursor[0] -= 1;
        break;
    }
  };

  const overBoundary = () =>
    cursor[1] > boundary.right ||
    cursor[0] > boundary.bottom ||
    cursor[1] < boundary.left ||
    cursor[0] < boundary.top;

  while (!overBoundary()) {
    result.push(matrix[cursor[0]][cursor[1]]);

    switch (direction) {
      case 'right':
        if (cursor[1] + 1 > boundary.right) {
          boundary.top += 1;
          direction = 'bottom';
        }
        break;
      case 'bottom':
        if (cursor[0] + 1 > boundary.bottom) {
          boundary.right -= 1;
          direction = 'left';
        }
        break;
      case 'left':
        if (cursor[1] - 1 < boundary.left) {
          boundary.bottom -= 1;
          direction = 'top';
        }
        break;
      case 'top':
        if (cursor[0] - 1 < boundary.top) {
          boundary.left += 1;
          direction = 'right';
        }
        break;
    }

    walk(direction);
  }

  return result;
};
Last Updated: 7/2/26, 2:05 AM
Contributors: henri.zhang
Prev
48. 旋转图像
Next
55. 跳跃游戏