《LeetCode力扣练习》代码随想录——数组(螺旋矩阵II---Java)

《LeetCode力扣练习》代码随想录------数组(螺旋矩阵II---Java)



刷题思路来源于 代码随想录

59. 螺旋矩阵 II
左闭右开------[x,y)
java 复制代码
class Solution {
    public int[][] generateMatrix(int n) {

        if(n==1){
            return new int[][]{{1}};
        }

        int[][] result=new int[n][n];
        int start=0;
        int row=-1;
        int col=-1;
        int loop=0;
        int offset=1;
        int count=1;

        for(;loop<(n/2);loop++){

            for(col=start;col<(n-offset);col++){
                result[start][col]=count++;
            }

            for(row=start;row<(n-offset);row++){
                result[row][col]=count++;
            }

            for(;col>start;col--){
                result[row][col]=count++;
            }

            for(;row>start;row--){
                result[row][col]=count++;
            }

            start++;
            offset++;

        }

        if(n%2==1){
            result[start][start]=count;
        }

        return result;

    }
}
54. 螺旋矩阵
左闭右开------[x,y)
java 复制代码
class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {

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

        if(m==1&&n==1){
            return Collections.singletonList(matrix[0][0]);
        }

        List<Integer> result=new ArrayList<>();
        
        int startRow=0;
        int startCol=0;

        int row=-1;
        int col=-1;

        int offset=1;
        int loop=m<n?(m/2):(n/2);

        for(;loop>0;loop--){

            for(col=startCol;col<(n-offset);col++){
                result.add(matrix[startRow][col]);
            }

            for(row=startRow;row<(m-offset);row++){
                result.add(matrix[row][col]);
            }

            for(;col>startCol;col--){
                result.add(matrix[row][col]);
            }

            for(;row>startRow;row--){
                result.add(matrix[row][col]);
            }

            startRow++;
            startCol++;

            offset++;

        }

        if((m<n?m:n)%2==1){

            if(n>m){
                for(col=startCol;col<=(n-offset);col++){
                    result.add(matrix[startRow][col]);
                }
            }else{
                for(row=startRow;row<=(m-offset);row++){
                    result.add(matrix[row][startCol]);
                }
            }

        }

        return result;

    }
}
LCR 146. 螺旋遍历二维数组
左闭右开------[x,y)
java 复制代码
class Solution {
    public int[] spiralArray(int[][] array) {

        int m=array.length;
        
        if(m==0){
            return new int[]{};
        }

        int n=array[0].length;

        if(m==1&&n==1){
            return new int[]{array[0][0]};
        }

        int[] result=new int[m*n];

        int startRow=0;
        int startCol=0;

        int row=-1;
        int col=-1;

        int offset=1;
        int loop=(m<n?m:n)/2;
        int count=0;

        for(;loop>0;loop--){

            for(col=startCol;col<(n-offset);col++){
                result[count++]=array[startRow][col];
            }

            for(row=startRow;row<(m-offset);row++){
                result[count++]=array[row][col];
            }

            for(;col>startCol;col--){
                result[count++]=array[row][col];
            }

            for(;row>startRow;row--){
                result[count++]=array[row][col];
            }

            offset++;

            startRow++;
            startCol++;

        }

        if((m<n?m:n)%2==1){

            if(n<m){
                for(row=startRow;row<=(m-offset);row++){
                    result[count++]=array[row][startCol];
                }
            }else{
                for(col=startCol;col<=(n-offset);col++){
                    result[count++]=array[startRow][col];
                }
            }

        }

        return result;

    }
}

相关推荐
DdddJMs__1358 分钟前
C语言 | Leetcode C语言题解之第458题可怜的小猪
c语言·leetcode·题解
哎呀呀嗯呀呀14 分钟前
class 031 位运算的骚操作
java·算法·位运算
2401_8581205317 分钟前
古典舞在线交流平台:SpringBoot设计与实现详解
java·spring boot·后端
大白飞飞25 分钟前
IDEA创建、导入、删除maven项目
java·maven·intellij-idea
赐你岁月如歌29 分钟前
如何使用ssm实现基于web的网站的设计与实现+vue
java·后端·ssm
2401_857297911 小时前
秋招内推2025-招联金融
java·前端·算法·金融·求职招聘
一 乐1 小时前
考研论坛平台|考研论坛小程序系统|基于java和微信小程序的考研论坛平台小程序设计与实现(源码+数据库+文档)
java·数据库·学习·考研·微信·小程序·源码
一 乐1 小时前
租拼车平台|小区租拼车管理|基于java的小区租拼车管理信息系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·微信·notepad++·拼车
xmh-sxh-13142 小时前
如何选择数据库架构
java
jxxchallenger2 小时前
踩坑spring cloud gateway /actuator/gateway/refresh不生效
java·数据库·gateway