【leetcode-数组】螺旋矩阵

题目:

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

示例 1:

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

示例 2:

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

思路:

一圈一圈的往里遍历, 分别是上、右、下、左

java代码:

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<>();

        if (matrix == null || matrix.length == 0) {
            return res;
        }

        int m = matrix.length;
        int n = matrix[0].length;

        //初始化扫描的行和列
        int row = 0;
        int col = -1;

        for (int i = 0; i <= m / 2; i++) {//圈层
            //扫描过的,把matrix[row][col]设置为Integer.MIN_VALUE
            while (col + 1 < n && matrix[row][col + 1] != Integer.MIN_VALUE) { //圈的上方行(从左到右扫描)
                col++;
                res.add(matrix[row][col]);
                matrix[row][col] = Integer.MIN_VALUE;
            }

            while (row+1<m && matrix[row+1][col]!=Integer.MIN_VALUE) { //圈的右边列(从上到下扫描)
                row++;
                res.add(matrix[row][col]);
                matrix[row][col] = Integer.MIN_VALUE;
            }

            while (col-1>=0 && matrix[row][col-1]!=Integer.MIN_VALUE) {//圈的下边行(从右到左扫描)
                col--;
                res.add(matrix[row][col]);
                matrix[row][col] = Integer.MIN_VALUE;
            }

            while (row-1>=0 && matrix[row-1][col]!=Integer.MIN_VALUE) {//圈的左边列(从下到上扫描)
                row--;
                res.add(matrix[row][col]);
                matrix[row][col] = Integer.MIN_VALUE;
            }

        }

        return res;
    }
}

 

由于水平有限,文章中难免会有一些错误,有纰漏之处恳请各位大佬不吝赐教!

及时更新最新文章和学习资料,一起来学习:

推荐阅读:

【leetcode-字符串】二进制求和

【leetcode-数组】买卖股票的最佳时机 II  - 优快云博客

【leetcode-数组】加一  - 优快云博客

【leetcode-数组】 旋转数组  - 优快云博客

【leetcode-数组】移动零  - 优快云博客

【leetcode-数组】两个数组的交集 II  - 优快云博客

【leetcode】搜索旋转排序数组  - 优快云博客

 

 

### 螺旋矩阵的概念 螺旋矩阵是一种特殊的二维数组结构,其中元素按照从外向内的顺时针顺序排列。这种排列方式使得遍历时呈现出一种螺旋状路径。 ### C# 中的螺旋矩阵实现 为了在C#中实现打印螺旋矩阵的功能,可以采用如下方法: ```csharp public class SpiralMatrix { public static void PrintSpiral(int[,] matrix, int rows, int cols) { int top = 0; int bottom = rows - 1; int left = 0; int right = cols - 1; while (top <= bottom && left <= right) { // 打印顶部行 for (int i = left; i <= right; ++i) Console.Write(matrix[top,i] + " "); top++; // 打印右侧列 for (int i = top; i <= bottom; ++i) Console.Write(matrix[i,right] + " "); right--; if (top <= bottom) { // 打印底部行 for (int i = right; i >= left; --i) Console.Write(matrix[bottom,i] + " "); bottom--; } if (left <= right) { // 打印左侧列 for (int i = bottom; i >= top; --i) Console.Write(matrix[i,left] + " "); left++; } } } } ``` 此代码定义了一个`PrintSpiral`函数用于按螺旋顺序打印给定矩形区域中的整数[^1]。 ### JavaScript 中的螺旋矩阵遍历 对于JavaScript而言,同样可以通过控制边界条件来完成螺旋矩阵的遍历操作: ```javascript function spiralOrder(matrix) { let result = []; if (!matrix.length || !matrix[0].length) return result; let rowBegin = 0, rowEnd = matrix.length - 1, colBegin = 0, colEnd = matrix[0].length - 1; while (rowBegin <= rowEnd && colBegin <= colEnd) { // 向右移动直到最右边 for (let j = colBegin; j <= colEnd; j++) result.push(matrix[rowBegin][j]); rowBegin++; // 向下移动直到最底端 for (let i = rowBegin; i <= rowEnd; i++) result.push(matrix[i][colEnd]); colEnd--; if (rowBegin <= rowEnd) { // 向左移动回到起点前一位 for (let j = colEnd; j >= colBegin; j--) result.push(matrix[rowEnd][j]); rowEnd--; } if (colBegin <= colEnd) { // 向上移动回到起始位置之上 for (let i = rowEnd; i >= rowBegin; i--) result.push(matrix[i][colBegin]); colBegin++; } } return result; } ``` 这段脚本展示了如何通过调整四个边界的范围逐步缩小访问区域直至全部元素被处理完毕[^2]。 ### Python 的解决方案 Python版本则更加强调简洁性和表达力,下面是一段典型的螺旋矩阵遍历逻辑: ```python def spiral_order(matrix): res = [] while matrix: # 取出顶层并移除该层 res += matrix.pop(0) # 将剩余部分逆时针旋转90度继续迭代 if matrix and matrix[0]: for row in list(zip(*matrix))[::-1]: matrix.append(list(row)) del matrix[0] return res ``` 这里利用了列表推导式以及内置函数`zip()`实现了较为直观的操作流程[^3]。 ### C语言下的具体实践 而在C语言环境下,由于缺乏高级特性支持,通常会更加依赖指针运算和手动管理内存空间来进行类似的任务: ```c #include <stdio.h> void print_spiral(int mat[][N], int m, int n){ int i,k=0,l=0; /* k - starting row index l - starting column index */ while (k<m && l<n){ // 打印当前行的第一个元素至最后一个元素 for(i=l;i<n;++i){ printf("%d ",mat[k][i]); } k++; // 打印最后一列除了第一个元素以外的所有元素 for(i=k;i<m;++i){ printf("%d ",mat[i][n-1]); } n--; if(k<m){ // 打印倒数第二行除去两端之外的所有元素 for(i=n-1;(i>=l);--i){ printf("%d ",mat[m-1][i]); } m--; } if(l<n){ // 打印首列除去上下两个极端值之间的所有元素 for(i=m-1;i>=k;--i){ printf("%d ",mat[i][l]); } l++; } } } ``` 上述片段提供了一种基于循环体内部嵌套if判断的方式来确保每次只处理有效区间的数据[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值