【CT】LeetCode手撕—54. 螺旋矩阵

题目


1- 思路

  • 模式识别:螺旋矩阵 ——> 用四个指针来顺时针遍历

2- 实现

⭐54. 螺旋矩阵——题解思路

在这里插入图片描述

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

        // 定义四个指针
        int left = 0;
        int right = matrix[0].length-1;
        int top = 0;
        int bottom = matrix.length-1;

        while(true){

            for(int i = left ; i <= right;i++){
                res.add(matrix[top][i]);
            }
            top++;
            if(top>bottom) break;

            for(int i = top;i<= bottom;i++){
                res.add(matrix[i][right]);
            }
            right--;
            if(left>right) break;

            for(int i = right ; i>= left;i--){
                res.add(matrix[bottom][i]);
            }
            bottom--;
            if(bottom<top) break;

            for(int i = bottom ; i>=top;i--){
                res.add(matrix[i][left]);
            }
            left++;
            if(left>right) break;
        }
        return res;
    }
}

3- ACM实现

public class spiralOrder {

    public static List<Integer>  spiral(int[][] matrix){
        List<Integer> res = new ArrayList<>();
        // 四个指针
        int left = 0;
        int right = matrix[0].length-1;
        int top = 0;
        int buttom = matrix.length-1;

        // 2. 开始遍历
        while(true){

            // 第一行
            for(int i = left;i<=right;i++){
                res.add(matrix[top][i]);
            }
            top++;
            if(top>buttom) break;

            // 右侧第一列
            for(int i = top;i<=buttom;i++){
                res.add(matrix[i][right]);
            }
            right--;
            if(right<left) break;

            // 底下一行
            for(int i = right; i>= left;i--){
                res.add(matrix[buttom][i]);
            }
            buttom--;
            if(buttom<top) break;

            // 左侧一列
            for(int i = buttom;i>=top;i--){
                res.add(matrix[i][left]);
            }
            buttom--;
            if(buttom<top) break;
        }
        return res;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入二维数组的行");
        int m = sc.nextInt();
        System.out.println("输入二维数组的列");
        int n = sc.nextInt();
        int[][] matrix = new int[m][n];
        System.out.println("输入二维数组的元素");
        for (int i = 0 ; i < m;i++){
            for(int j = 0 ; j < n ;j++){
                matrix[i][j] = sc.nextInt();
            }
        }
        List<Integer> forRes = spiral(matrix);
        for(int i : forRes){
            System.out.print(i+" ");
        }
    }
}

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值