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 ]
]
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > res(n,vector<int>(n,));
int dx[] = {,,,-};
int dy[] = {,,-,};
int cnt = ,step = ,x = , y = ;
while(cnt <= n*n){
step%=;
res[x][y] = cnt++;
if(x+dx[step] >=n || x+dx[step] < || y+dy[step] >=n || y+dy[step] < || res[x+dx[step]][y+dy[step]]) step++;
x+=dx[step%];y+=dy[step%];
}
return res;
}
};

Leetcode Spiral Matrix II的更多相关文章

  1. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  2. [LeetCode] Spiral Matrix II 螺旋矩阵之二

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

  3. [Leetcode] spiral matrix ii 螺旋矩阵

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

  4. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  5. LeetCode Spiral Matrix II (技巧)

    题意: 从1开始产生连续的n2个数字,以螺旋的方式填满一个n*n的数组. 思路: 由于是填满一个矩阵,那么只需要每次都填一圈即可.应该注意特殊情况. 迭代: class Solution { publ ...

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

  7. 【leetcode】Spiral Matrix II

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

  8. LeetCode:Spiral Matrix I II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

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

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

随机推荐

  1. php文件上传进度条例子

    <?php session_start(); ?> <!DOCTYPE html> <html lang="zh-CN"> <head&g ...

  2. 【翻译十二】java-并发之活性

    A concurrent application's ability to execute in a timely manner is known as its liveness. This sect ...

  3. 无废话ExtJs 入门教程十[单选组:RadioGroup、复选组:CheckBoxGroup]

    无废话ExtJs 入门教程十[单选组:RadioGroup.复选组:CheckBoxGroup] extjs技术交流,欢迎加群(201926085) 继上一节内容,我们在表单里加了个一个单选组,一个复 ...

  4. 浅析配置更快的Eclipse方法

    很多人感觉自己的elipse启动比较慢,其实并不是因为装的插件太多或者是导入的项目有点大,而是因为参数的设置不合理导致的.可以在eclipse.ini里面添加-Xloggc:gc.log看看启动的日志 ...

  5. font-face使用备忘

    @font-face { font-family: 'SingleMalta'; src: url('./font/SingleMalta.ttf'); } @font-face { font-fam ...

  6. golang level

    exp = (currentLevel-1) * 501 02 503 1004 150startLevel = 1currentLevel = 2currentExp = 0

  7. bootstrap 入门

    bootstrap 入门 <!DOCTYPE html> <html> <head lang="en"> <meta charset=&q ...

  8. The Basics of 3D Printing in 2015 - from someone with 16 WHOLE HOURS' experience

    全文转载自 Scott Hanselman的博文. I bought a 3D printer on Friday, specifically a Printrbot Simple Metal fro ...

  9. 认识B/S架构

    Browser/Server即浏览器/服务器模式. Web浏览器主要功能 1. 申请服务,包括服务器的Ip地址和文件 2. 从服务器上下载 3. 解析下载的文件 4. 用过http协议进行通信 Web ...

  10. 【java基础】内存分析

    在上次我们说的<重载与重写>呢,我们遗留了一个问题,就是运行结果的各异性,那今天,我们就来探究一下         内存里的天地.                  首先呢,我们把mian ...