给出正整数 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. unable to create new native thread 问题

    ulimit -a ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling pr ...

  2. 使用OGNL表达式

    OGNL表达式(#号的用法) 用法1:访问OGNL上下文和Action上下文,#相当于ActionContext.getContext() 1.如果访问其他Context中的对象,由于他们不是根对象, ...

  3. javaScript的类型转换

    1.javaScript会自动跟据期望将值进行转换,比如 2.下面表列出了一些javaScript的自动转换,其中粗体字表示了出乎意料的转换情况 3.显示的类型转换 尽管类型可以自动进行一些转换,但是 ...

  4. highcharts 图例全选按钮方法

    $('#uncheckAll').click(function(){ var chart = $('#container').highcharts(); var series = chart.seri ...

  5. 「LuoguP2365」 任务安排(dp

    题目描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时间是Ti. 在每批任务开 ...

  6. Codeforces Round #394 (Div. 2) 颓废记

    昨天晚上(今天凌晨),又忍不住去打CF.(本蒟弱到只能打Div.2)... 我觉得我可以用一个词概括我这次的CF: 呵呵 刚一开赛,我就codeforces访问失败.. 后来好不容易能上了,两三分钟才 ...

  7. QT(1)介绍

    Qt官网 Qt官网:https://www.qt.io Qt下载:http://www.qt.io/download Qt所有下载:http://download.qt.io/archive/qt Q ...

  8. POJ1006Biorhythms——中国剩余定理

    题目:http://poj.org/problem?id=1006 用扩展欧几里得算法求逆元,使用中国剩余定理: 本题较简单,可以手算直接写出,不过我仍使用了模板. 代码如下: #include< ...

  9. MYSQL学习二 关于左连接

    工作中有如下的SQL, 针对A.ID ='abcdefg', left  join  B和C两个表,来查找其他信息.就算是B和C中没有任何满足条件的记录,最后结果也肯定不是空.因为A.ID ='abc ...

  10. [xdoj1233]Glory and LCS

    题意:求两个排列的最长公共子序列n<=1e5 解题关键:转化为LIS. 最长公共子序列 的 nlogn 的算法本质是 将该问题转化成 最长增序列(LIS),因为 LIS 可以用nlogn实现,所 ...