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

    }
}

相关推荐
SimonLiu00910 分钟前
清理HiNas(海纳斯) Docker日志并限制日志大小
java·docker·容器
带刺的坐椅11 分钟前
开发 MCP Proxy(代理)也可以用 Solon AI MCP 哟!
java·ai·llm·solon·mcp·mcp-server·mcp-client
yuren_xia27 分钟前
Spring XML 配置
xml·java·spring
mit6.82441 分钟前
[贪心_7] 最优除法 | 跳跃游戏 II | 加油站
数据结构·算法·leetcode
小鸡脚来咯43 分钟前
SpringBoot 常用注解大全
java
风铃儿~1 小时前
Java面试高频问题(26-28)
java·算法·面试
IT瘾君1 小时前
Java基础:认识注解,模拟junit框架
java·开发语言·junit
风象南1 小时前
SpringBoot中3种内容协商策略实现
java·spring boot·后端
IT瘾君1 小时前
JavaWeb:Web介绍
java·web
不秃的开发媛1 小时前
前端技术Ajax入门
java·开发语言·前端