给出正整数 n,生成正方形矩阵,矩阵元素为 1 到 n2 ,元素按顺时针顺序螺旋排列。
例如,
给定正整数 n = 3,
应返回如下矩阵:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]
详见:https://leetcode.com/problems/spiral-matrix-ii/description/

Java实现:

class Solution {
public int[][] generateMatrix(int n) {
int[][] res=new int[n][n];
if(n==0){
return res;
}
int top=0;
int bottom=n-1;
int left=0;
int right=n-1;
int num=1;
while(top<=bottom&&left<=right){
if(num<=n*n){
for(int j=left;j<=right;++j){
res[top][j]=num;
++num;
}
}
++top;
if(num<=n*n){
for(int i=top;i<=bottom;++i){
res[i][right]=num;
++num;
}
}
--right;
if(num<=n*n){
for(int j=right;j>=left;--j){
res[bottom][j]=num;
++num;
}
}
--bottom;
if(num<n*n){
for(int i=bottom;i>=top;--i){
res[i][left]=num;
++num;
}
}
++left;
}
return res;
}
}

059 Spiral Matrix II 旋转打印矩阵 II的更多相关文章

  1. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

  2. 059. Spiral Matrix II

    题目链接:https://leetcode.com/problems/spiral-matrix-ii/description/ Given a positive integer n, generat ...

  3. Java for LeetCode 059 Spiral Matrix II

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

  4. 054 Spiral Matrix 旋转打印矩阵

    给出一个 m x n 的矩阵(m 行, n 列),请按照顺时针螺旋顺序返回元素.例如,给出以下矩阵:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]应该返回 [1,2, ...

  5. 【LeetCode每天一题】Spiral Matrix II(螺旋数组II)

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

  6. 【leetcode刷题笔记】Spiral Matrix II

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

  7. [Leetcode] spiral matrix ii 螺旋矩阵

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

  8. [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 ...

  9. C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...

随机推荐

  1. CodeForces - 1000E :We Need More Bosses(无向图缩点+树的直径)

    Your friend is developing a computer game. He has already decided how the game world should look lik ...

  2. SET_Matplotlib

    fig.tight_layout() 调整子图间距 legend 图例分开显示

  3. BZOJ3524:[POI2014]Couriers

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...

  4. bzoj1072Perm——状压DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1072 数字串最多只有10位,所以考虑用状压: 压缩的状态是哪些数字被用过,这样可以从一种状态 ...

  5. 2011年浙大:Twin Prime Conjecture

    Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. PKI标准、CA采取的规范、X509、PKCS

    PKI:Public Key Infrastructure 公钥基础设施 PKI标准可以分为第一代和第二代标准. 第一代PKI标准主要包括美国RSA公司的公钥加密标准(Public Key Crypt ...

  7. 从头开始学Web开发—CSS_01

    CSS导入使用及引用的两种方法: 外部引用 外部引用的方式,我们直接可以通过link标签来引用我们写好的一个CSS文件: 在link 标签中,我们通过rel = "stylesheet&qu ...

  8. jenkins 找不到mvn 命令

    错误如下: /data/jenkins/temp/hudson9132559581388971644.sh: line 4: mvn: command not found 方法如下: 1  修改环境变 ...

  9. DOTween Sequence 使用图解

    http://blog.csdn.net/jiejieup/article/details/41521577 最近在使用DOTween制作一些动画过渡的内容,发现非常好用,使用Sequence类可以方 ...

  10. 以太坊开发教程(二) 利用truffle发布宠物商店 DAPP 到 以太坊测试环境Ropsten

    1.环境安装 1) node安装 设置镜像地址: curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -下载安装 ...