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. 曲线行驶s弯道技巧图解【转】

    s弯道怎么走?在走S弯的时候,最主要的就是控制车的速度,在做每个动作的时候要保持一样的速度,不要一会快一会慢的,在开的时候,因为每个人的身高,体型不一样,每个人看的点位都是不一样的,每次在开的时候要找 ...

  2. Android4.4中不能发送SD卡就绪广播

    当在Android上进行图片的扫描功能开发时一般会使用:sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(“file:// ...

  3. 求第N数大问题

    问题: InputThe first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of ...

  4. easyVS

    easyVS 详细说明点这里

  5. [NOIP1997] P2626 斐波那契数列(升级版)

    题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数). 题目描述 ...

  6. sdibt 1244 烦人的幻灯片

    在这个OJ站还没号,暂时没提交,只是过了样例 真不愧是烦人的幻灯片,烦了我一小时 ---更新:OJ测试完毕,AC 烦人的幻灯片问题 Time Limit: 1 Sec  Memory Limit: 6 ...

  7. Linux下J2EE环境搭建

    1.下载MyEclipse 2010的linux安装包. myeclipse-10.1-offline-installer-linux 2.将下载MyEclipse 2010的linux安装包,使用X ...

  8. form的submit与onsubmit的用法与区别

    发生顺序:onsubmit -> submit1.阻止表单提单:<script>function submitFun(){    //逻辑判断    return true; //允 ...

  9. 如何把项目托管到GitHub

    第一步:先注册一个Github的账号,这是必须的 注册地址:Github官网注册入口 第二步:准备工作 gitHub网站使用Git版本管理工具来对仓库进行管理,注意它们并不等同. gitHub是全球最 ...

  10. memcache的内存回收机制

    memcache不会释放内存,而是重新利用. 在缓存的清除方面,memcache是不释放已分配内存.当已分配的内存所在的记录失效后,这段以往的内存空间,memcache只会重复利用. memcache ...