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 ]
]

解题思路:

参考Java for LeetCode 054 Spiral Matrix,修改下代码即可,JAVA实现如下:

static public int[][] generateMatrix(int n) {
if(n==0)
return new int[0][0];
int[][] matrix=new int[n][n];
int num=0,left = 0, right = matrix[0].length - 1, up = 0, down = matrix.length - 1;
while (left <= right && up <= down) {
for (int i = left; i <= right&&up<=down; i++)
matrix[up][i]=++num;
up++;
for (int i = up; i <= down&&left<=right; i++)
matrix[i][right]=++num;
right--;
for (int i = right; i >= left&&up<=down; i--)
matrix[down][i]=++num;
down--;
for (int i = down; i >= up&&left<=right; i--)
matrix[i][left]=++num;
left++;
}
return matrix;
}

Java for LeetCode 059 Spiral Matrix II的更多相关文章

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

  2. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  3. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  4. 059. Spiral Matrix II

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

  5. 【leetcode】Spiral Matrix II (middle)

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

  6. Leetcode#59 Spiral Matrix II

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

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

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

  8. Java for LeetCode 054 Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. LeetCode 58 Spiral Matrix II

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

随机推荐

  1. Cocos2d-X3.0 刨根问底(九)----- 场景切换(TransitionScene)源码分析

    上一章我们分析了Scene与Layer相关类的源码,对Cocos2d-x的场景有了初步了解,这章我们来分析一下场景变换TransitionScene源码. 直接看TransitionScene的定义 ...

  2. 【poj3608】 Bridge Across Islands

    http://poj.org/problem?id=3608 (题目链接) 题意 求两凸包间最短距离 Solution 难写难调,旋转卡壳,还真是卡死我了. 先分别选出两凸包最上点和最下点,从这两点开 ...

  3. shopex商城的部署和安装

    1.在网站上下载最新的压缩包: 2.shopex是商业软件,不开源,且源代码是加密的! 3.如果你出现zend的错误,是因为你的php环境没有安装此插件,推荐使用phpstudy最新版本,我使用的ph ...

  4. UVa 1347 Tour

    Tour Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Description   Joh ...

  5. UVALive 6523 Languages

    传送门 The Enterprise has encountered a planet that at one point had been inhabited. The only remnant f ...

  6. C#实现通过程序自动抓取远程Web网页信息的代码

    http://www.jb51.net/article/9499.htm 通过程序自动的读取其它网站网页显示的信息,类似于爬虫程序.比方说我们有一个系统,要提取BaiDu网站上歌曲搜索排名.分析系统在 ...

  7. data URI

    参考资料:http://www.cnblogs.com/hustskyking/p/data-uri.html 与http,ftp等协议类似,data URL也是一种协议,不同的是它直接将数据(编码或 ...

  8. 不引用office动态库导出excel

    public class OutExcelReport { /// <summary> /// 把 DataSet 的数据导成 Excel /// </summary> /// ...

  9. php 开启curl,重启php-fpm服务

    1,找到php.ini配置 find / -name 'php.ini' /usr/local/php/etc/php.ini 找到extension=php_curl.dll 把前面的分号去掉即可. ...

  10. wordpress编辑主题时报错Warning: scandir() has been disabled for security reasons in

    在ubuntu下面安装了一个wordpress程序,在后台什么都没干,编辑主题时,发现页面中报下面的错误. notice: /home/wwwroot/test.localhost/wordpress ...