二维数组的遍历

旋转图像

class Solution {
    public void rotate(int[][] matrix) {
        for(int i=0;i<matrix.length;i++){
            for(int j=i+1;j<matrix[0].length;j++){
                int temp=matrix[i][j];
                matrix[i][j]=matrix[j][i];
                matrix[j][i]=temp;
            }
        }
        for(int[] arr:matrix){
            reverse(arr);
        }
    }
    void reverse(int[] arr){
            int i=0,j=arr.length-1;
            while(i<j){
                int temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
                i++;
                j--;
            }
        }
}

注意,j要从i开始 避免重复反转!

螺旋矩阵

先定义出四个边界点。按照上-右-下-左方式遍历,遍历完慢慢压缩,是左闭右闭区间

遍历哪条边,就压缩哪一条边,遍历上就往下压缩,遍历右就向左压缩

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        int m=matrix.length;
        int n=matrix[0].length;
        int tog=m*n;
        int up=0,down=m-1,left=0,right=n-1;
        List<Integer> res=new ArrayList<>();
        while(res.size()<tog){
            if(up<=down){
                for(int j=left;j<=right;j++){
                    res.add(matrix[up][j]);
                }
                up++;
            }
            if(right>=left){
                for(int i=up;i<=down;i++){
                    res.add(matrix[i][right]);
                }
                right--;
            }
            if(down>=up){
                for(int j=right;j>=left;j--){
                    res.add(matrix[down][j]);
                }
                down--;
            }
            if(left<=right){
                for(int i=down;i>=up;i--){
                    res.add(matrix[i][left]);
                }
                left++;
            }
        }
        return res;
    }
}
class Solution {
    public int[][] generateMatrix(int n) {
        int[][] matrix=new int[n][n];
        int left=0,right=n-1,up=0,down=n-1;
        int count=1;//初始数
        while(count<=n*n){
            if(up<=down){
                for(int j=left;j<=right;j++){
                    matrix[up][j]=count++;
                }
                up++;
            }
            if(right>=left){
                for(int i=up;i<=down;i++){
                    matrix[i][right]=count++;
                }
                right--;
            }
            if(down>=up){
                for(int j=right;j>=left;j--){
                    matrix[down][j]=count++;
                }
                down--;
            }
            if(left<=right){
                for(int i=down;i>=up;i--){
                    matrix[i][left]=count++;
                }
            }
            left++;
        }
        return matrix;
    }
}

相关推荐

  1. 2024-06-14 18:26:01       55 阅读
  2. 和里面对象

    2024-06-14 18:26:01       58 阅读
  3. uniapp对象常见方法

    2024-06-14 18:26:01       29 阅读
  4. Linux Shell,或文件几种不同写法

    2024-06-14 18:26:01       33 阅读
  5. Flutter循环获取索引值

    2024-06-14 18:26:01       68 阅读
  6. vue v-for 同时两个

    2024-06-14 18:26:01       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-14 18:26:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-14 18:26:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-14 18:26:01       82 阅读
  4. Python语言-面向对象

    2024-06-14 18:26:01       91 阅读

热门阅读

  1. 实时通信websocket和sse

    2024-06-14 18:26:01       24 阅读
  2. 在vue和uniapp中使用 websocket并封装js

    2024-06-14 18:26:01       33 阅读
  3. Linux 常用命令合集

    2024-06-14 18:26:01       26 阅读
  4. React的@reduxjs/toolkit的异步方法处理和实现

    2024-06-14 18:26:01       27 阅读
  5. 【Go】使用Go语言实现AES CBC No Padding加密和解密

    2024-06-14 18:26:01       31 阅读
  6. Redis缓存(笔记三:Redis6新数据类型)

    2024-06-14 18:26:01       27 阅读
  7. junit mockito Dao层

    2024-06-14 18:26:01       32 阅读
  8. 如何对Spring管理bean进行增强

    2024-06-14 18:26:01       36 阅读
  9. cloud compare编译

    2024-06-14 18:26:01       25 阅读