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

    }
}

相关推荐
uppp»25 分钟前
深入理解 Java 反射机制:获取类信息与动态操作
java·开发语言
m0_748256144 小时前
SpringBoot
java·spring boot·后端
阿华的代码王国4 小时前
【从0做项目】Java搜索引擎(3)
java·搜索引擎·项目
Mr.朱鹏4 小时前
针对Feign客户端请求体参数处理问题
java·jvm·spring boot·spring·spring cloud·maven·intellij-idea
牛大了20235 小时前
[LeetCode力扣hot100]-二叉树相关手撕题
算法·leetcode·职场和发展
ll7788115 小时前
LeetCode每日精进:20.有效的括号
c语言·开发语言·算法·leetcode·职场和发展
德先生&赛先生5 小时前
LeetCode-633. 平方数之和
数据结构·算法·leetcode
涛粒子6 小时前
Spring Bean 生命周期的执行流程
java·后端·spring
刘_sy7 小时前
使用EasyExcel和多线程实现高效数据导出
java·excel·easyexcel·批量导出excel
梦幻通灵7 小时前
IDEA通过Contince接入Deepseek
java·ide·intellij-idea