1. 原题链接

https://leetcode.com/problems/spiral-matrix-ii/description/

2. 题目要求

给定一个正整数n,求出从1到n平方的螺旋矩阵。例如n为3,构成的螺旋矩阵如下图所示

3. 解题思路

该题与54题Spiral Matrix的解题思路大致相同,同样是一个while循环内,确定螺旋矩阵一个圈上的所有元素。

采用一个计数器count,count初始为1,每确定一个位置上的元素,count加1,

4. 代码实现

public class SpiralMatrixII59 {
public static void main(String[] args) {
for(int []n:generateMatrix(3)){
for(int x:n)
System.out.print(x+" ");
System.out.println();
}
} /**
* t t t t t
* l ... r
* l r
* l ... r
* b b b b r
*/
public static int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
int left = 0, right = n - 1;
int top = 0, bottom = n - 1;
int count = 1;
while (left <=right) {
// 确定t所在边上的元素
for (int i = left; i <= right; i++) {
res[top][i] = count++;
}
top++;
// 确定r所在边上的元素
for (int i = top; i <= bottom; i++) {
res[i][right] = count++;
}
right--; // 确定b所在边上的元素
for(int i= right;i>=left;i--){
res[bottom][i]=count++;
}
bottom--;
// 确定l所在边上的元素
for(int i =bottom;i>=top;i--){
res[i][left]=count++;
}
left++;
}
return res;
}
}

  

LeetCode: 59. Spiral Matrix II(Medium)的更多相关文章

  1. 【leetcode】Spiral Matrix II (middle)

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

  2. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

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

  3. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

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

  4. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

  5. LeetCode Spiral Matrix II (技巧)

    题意: 从1开始产生连续的n2个数字,以螺旋的方式填满一个n*n的数组. 思路: 由于是填满一个矩阵,那么只需要每次都填一圈即可.应该注意特殊情况. 迭代: class Solution { publ ...

  6. [leetcode]59. Spiral Matrix II螺旋遍历矩阵2

    Given a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral or ...

  7. LeetCode: 63. Unique Paths II(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/

  8. 【leetcode】Single Number II (medium) ★ 自己没做出来....

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  9. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

随机推荐

  1. ojdbc14_g.jar与ojdbc14.jar区别

    在低级JDK版本1.2与1.3中使用的驱动,class12.jar,虽然实际上在1.4,1.5中使用大部分情况也是OK的 ojdbc14.jar (1,545,954 bytes) - classes ...

  2. hdu-4135 Co-prime---容斥定理经典&&求1-m中与n互质的数目

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 题目大意: 求区间[a, b]中与N互质的数目. 解题思路: 首先对n求出所有素因子. 对于区 ...

  3. Java实现身份证号码验证源码分享

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...

  4. Ubuntu中为Eclipse添加桌面启动快捷方式

    Ubuntu中应用程序启动器“XXX.desktop”还没有被标记为可信任的问题:http://www.tuicool.com/articles/fIBJ32n eclipse问题:prefences ...

  5. Nucleus进程间通信(IPC)方式

    版权声明:本文为博主原创文章,未经博主同意不得转载--"http://blog.csdn.net/suipingsp". https://blog.csdn.net/suiping ...

  6. luogu P3801 红色的幻想乡

    嘟嘟嘟 首先人人都能想到是线段树,不过二维线段树肯定会MLE+TLE的. 我们换一种想法,不去修改整个区间,而是修改一个点:开横竖两个线段树,分别记录哪些行和列被修改了.因为如果两阵红雾碰撞,则会因为 ...

  7. BZOJ 2818 GCD 【欧拉函数 || 莫比乌斯反演】

    传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=2818 2818: Gcd Time Limit: 10 Sec  Memory Limit ...

  8. 洛谷p1064 金明的预算方法

    有附带条件的01背包 要那附件必须拿主件 因为一个主件最多有两个附件,所以每次遇到主件可能有四种选择 1.只拿主件 2.拿主件和一号附件 3.拿主件和二号附件 4.都拿 #include <cs ...

  9. [19/04/08-星期一] 多线程_线程的优先级(Priority) 和 守护线程(Daemon)

    一.概念 1. 处于就绪状态的线程,会进入“就绪队列”等待JVM来挑选. 2. 线程的优先级用数字表示,范围从1到10,一个线程的缺省优先级是5. 3. 使用下列方法获得或设置线程对象的优先级. in ...

  10. 【转】maven命令-P 参数引发的思考

    序言: maven 命令:clean package -Dmaven.test.skip=true -P product 1.命令很简单是:清class文件,打包构建,跳过测试,注意最后一个 -P p ...