找LeetCode上简单的题目来撑过30天啦(DAY29)

题号:59 标题:Spiral Matrix II 难度:Medium

Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

Example 1:

Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]


Example 2:
Input: n = 1
Output: [[1]]


Constraints:
• 1 <= n <= 20


我的程序码

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        int i=0,j=0,k=1,ax=0,ay=0,bx=0,by=n-1,cx=n-1,cy=n-1,dx=n-1,dy=0;
        
        while(k!=(n*n+1)){
            
            for(i=ay;i<=by;i++){
                System.out.print(" 1:" + k);
                result[ax][i] = k;
                k++;
            }
            ax++;
            bx++;
            for(i=bx;i<=cx;i++){
                System.out.print(" 2:" + k);
                result[i][by] = k;
                k++;
            }
            by--;
            cy--;
            for(i=cy;i>=dy;i--){
                System.out.print(" 3:" + k);
                result[cx][i] = k;
                k++;
            }
            cx--;
            dx--;
            for(i=dx;i>=ax;i--){
                System.out.print(" 4:" + k);
                result[i][dy] = k;
                k++;
            }
            dy++;
            ay++;
        }
        
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                //System.out.print(result[i][j]);
            }
        }
        
        return result;
    }
}

day29心得
阵列越写越顺了呢,开心,明天就要结束拉,开心


<<:  30天学会C语言: Day 28-指标&阵列

>>:  30天学会 Python-Day28: 选择档案

D30-(9/30)-远雄港(5607)-贸易港区冷链

注:发文日和截图的日期不一定是同一天,所以价格计算上和当日不同,是很正常的。 声明:这一系列文章并无...

找LeetCode上简单的题目来撑过30天啦(DAY16)

ok,今天挑战同一张证照第三次失败了,有够难过的,但至少分述有越来越接近啦,再接再厉罗 题号:55 ...

AE-LED流动效果1-Day19

今天要来练习LED的流动练习 参考来源-六指渊:https://www.sixvfx.com/exp...

Day 07 Create a Regression Model with Azure Machine Learning designer

Regression - Predict numeric label based on item's...

[必学] iPhone、iPad 和 iPod 的 AirPlay 失灵的解决方法

AirPlay 自 iOS 5 开始在 iPhone 和 iPad 推出後,一直以简单有用着称。Ai...