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

Example:

Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]

题意:

给定n, 把1至n ^ 2所有的数,按照螺旋顺序填入方阵。

code

 /*
Time: O(n^2)
Space: O(n^2)
*/
class Solution {
public int[][] generateMatrix(int n) {
int[][] matrix = new int[n][n];
if (n == 0) return matrix;
int beginX = 0, endX = n - 1;
int beginY = 0, endY = n - 1;
int num = 1;
while (true) {
for (int j = beginX; j <= endX; ++j) matrix[beginY][j] = num++;
if (++beginY > endY) break; for (int i = beginY; i <= endY; ++i) matrix[i][endX] = num++;
if (beginX > --endX) break; for (int j = endX; j >= beginX; --j) matrix[endY][j] = num++;
if (beginY > --endY) break; for (int i = endY; i >= beginY; --i) matrix[i][beginX] = num++;
if (++beginX > endX) break;
}
return matrix;
}
}

[leetcode]59. Spiral Matrix II螺旋遍历矩阵2的更多相关文章

  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: 59. Spiral Matrix II(Medium)

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

  3. Leetcode#59 Spiral Matrix II

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

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

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

  5. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  6. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  7. [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

  8. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  9. 【leetcode】Spiral Matrix II

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

随机推荐

  1. spring redis 注解实现缓存机制

    1.xml配置 <bean id="poolConfigTax" class="redis.clients.jedis.JedisPoolConfig"& ...

  2. PythonStudy——函数对象 Function object

    # 在python中,所有变量存放的值只要是地址,我们就称之为对象# -- 所有的变量都是用来存放地址的,所以都是对象# -- 存放整型的地址就是整型对象 | 存放函数的地址就是函数对象 | 存放文件 ...

  3. how to tell gcc with c99 enable

    just copy the make file here. CC = gccCFLAGS = -Wall -std=c99OUTFILE = outputfileOBJS = source.oSRCS ...

  4. 前端 --- 5 BOM 和 DOM

    一.BOM BOM(Browser Object Model)是指浏览器对象模型, 它使 JavaScript 有能力与浏览器进行“对话”. 1. window 对象 一些常用的Window方法: ( ...

  5. 18.1 volatile的作用

    volatile的作用是作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值. 1.编译器的优化 在本次线程内,当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一 ...

  6. Azure SQL 数据库仓库Data Warehouse (4) 2018 TechSummit 动手实验营

    <Windows Azure Platform 系列文章目录> 上传一下之前在2018 TechSummit的动手实验营:Azure数据仓库PaaS项目架构规划与实战入门 包含PPT和Wo ...

  7. Delphi操作Ini文件

    Delphi提供了一个TInifile类,使我们可以非常灵活的处理INI文件 一.INI文件的结构[小节名]ini文件       关键字1=值1       关键子2=值2INI文件允许有多个小节, ...

  8. bui框架nav导航图标一览

    权限  .nav-permission   仓库  .nav-storage   库存  .nav-inventory   用户  .nav-user   订单  .nav-order   商品  . ...

  9. 解决Ubuntu中文显示为乱码

    1. 安装所需软件 sudo apt-get install zh-autoconvert sudo apt-get install zhcon 2. 配置系统 $ vi /var/lib/local ...

  10. [UE4]Tile View

    一.Tile View也属于List View,Tile View以小方格的形式展示子控件. 二.Tile View.Entry Height.Tile View.Entry Width设置每个Til ...