《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;

    }
}

相关推荐
写代码的小阿帆27 分钟前
Java体系总结——从基础语法到微服务
java·微服务·学习方法
SUPER52662 小时前
FastApi项目启动失败 got an unexpected keyword argument ‘loop_factory‘
java·服务器·前端
咕噜咕噜啦啦3 小时前
Eclipse集成开发环境的使用
java·ide·eclipse
光军oi6 小时前
全栈开发杂谈————关于websocket若干问题的大讨论
java·websocket·apache
weixin_419658316 小时前
Spring 的统一功能
java·后端·spring
小许学java6 小时前
Spring AI-流式编程
java·后端·spring·sse·spring ai
Asmalin7 小时前
【代码随想录day 35】 力扣 01背包问题 一维
算法·leetcode·职场和发展
剪一朵云爱着7 小时前
力扣2779. 数组的最大美丽值
算法·leetcode·排序算法
haogexiaole7 小时前
Java高并发常见架构、处理方式、api调优
java·开发语言·架构
EnCi Zheng7 小时前
@ResponseStatus 注解详解
java·spring boot·后端