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

    }
}

相关推荐
HelloWord~1 小时前
SpringSecurity+vue通用权限系统2
java·vue.js
让我上个超影吧1 小时前
黑马点评【基于redis实现共享session登录】
java·redis
BillKu2 小时前
Java + Spring Boot + Mybatis 插入数据后,获取自增 id 的方法
java·tomcat·mybatis
全栈凯哥2 小时前
Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解
java·算法·leetcode·链表
chxii2 小时前
12.7Swing控件6 JList
java
全栈凯哥2 小时前
Java详解LeetCode 热题 100(27):LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)详解
java·算法·leetcode·链表
YuTaoShao2 小时前
Java八股文——集合「List篇」
java·开发语言·list
SuperCandyXu2 小时前
leetcode2368. 受限条件下可到达节点的数目-medium
数据结构·c++·算法·leetcode
PypYCCcccCc2 小时前
支付系统架构图
java·网络·金融·系统架构
华科云商xiao徐2 小时前
Java HttpClient实现简单网络爬虫
java·爬虫