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

思路:与上一篇类似。仅仅要不越界就ok

public class Solution {
public int[][] generateMatrix(int n) {
if (n == 0)
return new int[0][0]; int[][] matrix = new int[n][n];
int x1 = 0;
int y1 = 0;
int x2 = matrix.length - 1;
int y2 = matrix[0].length - 1;
int i = 0, j = 1;
while (x1 <= x2 && y1 <= y2) {
// up row
for (i = y1; i <= y2; ++i, j++)
matrix[x1][i] = j;
// right column
for (i = x1 + 1; i <= x2; ++i, j++)
matrix[i][y2] = j;
// bottom row
for (i = y2 - 1; x2 != x1 && i >= y1; --i, j++)
matrix[x2][i] = j;
// left column
for (i = x2 - 1; y1 != y2 && i > x1; --i, j++)
matrix[i][y1] = j; x1++;
y1++;
x2--;
y2--;
}
return matrix;
}
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

LeetCode 58 Spiral Matrix II的更多相关文章

  1. 【leetcode】Spiral Matrix II

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

  2. 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 ...

  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(Medium)

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

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

  9. LeetCode题目:Spiral Matrix II

    原题地址:https://leetcode.com/problems/spiral-matrix-ii/ class Solution { public: vector<vector<in ...

随机推荐

  1. Hide the common top menu in Ubuntu 12.04

    隐藏:1.sudo apt-get autoremove appmenu-gtk appmenu-gtk3 appmenu-qt2.reboot 恢复: 1.sudo apt-get install ...

  2. Linux修改SSH连接数 重启SSH服务

    系统 linux,增加SSH终端连接数最大为1000个 解决方案: vi /etc/ssh/sshd_config 输入/MaxStartups 定位到如下并修改 1)        #MaxStar ...

  3. Java初转型-jdk安装和配置

    Java 开发环境配置 > * 下载JDK> * 配置环境变量> * 测试JDK是否安装成功> * 使用 Eclipse 运行第一个 Java 程序 下载JDK 首先我们需要下 ...

  4. django: urlconfig

    django 的 url 配置主要在 urls.py 中进行 urlconfig 中对 url 的处理方式主要在: 一 视图处理方式 如 上文 例子所示: url(r'^blog/index/$', ...

  5. oracle 优化 —— 分区表

    一.分区表简介 分区表类型:[范围分区].[列表分区] [hash分区]    [这些分区的组合分区] 范围分区:以某一个范围进行分区.eg:时间段划分. 列表分区:以某一些几个值进行分区.eg:地区 ...

  6. access 2007 vba 开发中学到的知识(一)

    使用ado连接本身的数据库,需要先创建一个 adodb.connection的连接对象 Set cn = CreateObject("ADODB.Connection") 数据库的 ...

  7. [Leetcode][015] 3Sum (Java)

    题目在这里: https://leetcode.com/problems/3sum/ [标签] Array; Two Pointers [个人分析] 老实交待,这个题卡半天,第一次做不会,抄别人的.过 ...

  8. sql_autoload_register() 函数 和__autoload() 的区别

    1:__autoload($class) 因为是一个函数,所以只能定义一次,使用多个会冲突报错;而 sql_autoload_register('function') 可定义多个,它有效地创建一个队列 ...

  9. CSS阻止文本选择

    在日常运用中,经常遇到点击按钮/菜单的时候,选中了文本,为了避免这种情况,可以使用纯css来解决这个问题(IE10+),对于旧版本的就只能用js:onselectstart = 'return fal ...

  10. instanceof操作符判断对象类型

    instanceof 的语法格式如下: myobject instanceof ExampleClass myobject:某类的对象引用 ExampleClass:某个类 class Quadran ...