首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Leetcode 题目解析之 Spiral Matrix II

Leetcode 题目解析之 Spiral Matrix II

原创
作者头像
ruochen
发布于 2022-01-10 12:08:58
发布于 2022-01-10 12:08:58
1.3K0
举报

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

For example,

Given n = 3,

You should return the following matrix:

[

1, 2, 3 ,

8, 9, 4 ,

7, 6, 5

]

代码语言:txt
AI代码解释
复制
    public int[][] generateMatrix(int n) {
        if (n <= 0) {
            return new int[0][0];
        }
        int[][] matrix = new int[n][n];
        int num = 1;
        int startx = 0, endx = n - 1;
        int starty = 0, endy = n - 1;
        while (startx <= endx && starty <= endy) {
            // 上边的行,从左向右
            for (int y = starty; y <= endy; y++) {
                matrix[startx][y] = num++;
            }
            // 右边的列,从上到下
            for (int x = startx + 1; x <= endx; x++) {
                matrix[x][endy] = num++;
            }
            // 如果行或列遍历完,则退出循环
            if (startx == endx || starty == endy) {
                break;
            }
            // 下边的行,从右向左
            for (int y = endy - 1; y >= starty; y--) {
                matrix[endx][y] = num++;
            }
            // 左边的列,从下到上
            for (int x = endx - 1; x >= startx + 1; x--) {
                matrix[x][starty] = num++;
            }
            startx++;
            starty++;
            endx--;
            endy--;
        }
        return matrix;
    }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Leetcode 题目解析之 Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
ruochen
2022/01/10
1.3K0
LeetCode 0059 - Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
Reck Zhang
2021/08/11
2010
Array - 59. Spiral Matrix II
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
ppxai
2020/09/23
2640
​LeetCode刷题实战59:螺旋矩阵 II
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2021/01/20
3400
​LeetCode刷题实战59:螺旋矩阵 II
C++版 - 剑指offer 面试题20:顺时针打印矩阵及其变形(LeetCode54. Spiral Matrix旋转矩阵) 题解
剑指offer 面试题20:顺时针打印矩阵 题目描述:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵:
Enjoy233
2019/03/05
1.2K0
C++版 - 剑指offer 面试题20:顺时针打印矩阵及其变形(LeetCode54. Spiral Matrix旋转矩阵) 题解
Leetcode 59 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 方阵蛇形填数 和上一道蛇形取数差不多。 http://blog.csdn.net/acc
triplebee
2018/01/12
4950
LeetCode 0054 - Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Reck Zhang
2021/08/11
1810
这个循环可以转懵很多人!
题目地址:https://leetcode-cn.com/problems/spiral-matrix-ii/ 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。
代码随想录
2021/05/11
6580
这个循环可以转懵很多人!
数组——59. 螺旋矩阵 II
给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
向着百万年薪努力的小赵
2022/12/02
4650
数组——59. 螺旋矩阵 II
leetcode: 59. Spiral Matrix II
Problem # Given an integer n, # generate a square matrix filled with elements from 1 to n2 in spiral order. # # For example, # Given n = 3, # # You should return the following matrix: # [ # [ 1, 2, 3 ], # [ 8, 9, 4 ], # [ 7, 6, 5 ] # ] AC cl
JNingWei
2018/09/27
3560
[leetcode]Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
全栈程序员站长
2022/07/06
2820
leetcode59螺旋矩阵II
给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。
用户11062199
2024/06/19
1060
包子小道消息@2019tech掌门人变迁小结 & Leetcode 59 solution: Spiral Matrix II
有人的地方就有江湖,2019年北美tech industry的掌门人也是几家欢喜几家愁,有人上位自然就有人离开,铁打的硬盘流水的兵。However, 这些帮主们早已经家财万贯,只是为了自己在wiki上面的履历再添一笔罢了,我等吃瓜群众看看热闹,默默YY一下如果有一天是我,喂,醒醒啦(#`O′)!
包子面试培训
2020/01/02
4150
LeetCode 59. Spiral Matrix IIsolution
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] Subscribe to see which companies asked this questi
desperate633
2018/08/22
2970
LeetCode 54 &59 Spiral MatrixI&II
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
大学里的混子
2018/11/04
6210
【leetcode两题选手】算法类题目(7.26)
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
看、未来
2020/08/25
3480
搜索二维矩阵 II(LeetCode 240)
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:
恋喵大鲤鱼
2024/01/05
1710
搜索二维矩阵 II(LeetCode 240)
LeetCode 题目解答——Medium 部分(下)
[Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复杂度上根本就不是最优解,有的写的太啰嗦,有的则用了一些过于 tricky 的方法。我没有为了这个再更新,就让它们去吧。
四火
2022/07/19
4330
LeetCode 题目解答——Medium 部分(下)
LeetCode - #54 螺旋矩阵
我们社区陆续会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。
Swift社区
2022/07/05
3430
LeetCode - #54 螺旋矩阵
Leetcode 54 Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. Subscribe to see which
triplebee
2018/01/12
5320
相关推荐
Leetcode 题目解析之 Spiral Matrix
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档