给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。

示例:

输入: 3 输出: [ [ 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, 0));
int left = 0;
int up = 0;
int right = n - 1;
int down = n - 1;
int cnt = 1;
while(up <= down && left <= right)
{
for(int i = left; i <= right; i++)
res[up][i] = cnt++;
up++;
if(up > down)
break;
for(int i = up; i <= down; i++)
res[i][right] = cnt++;
right--;
if(left > right)
break;
for(int i = right; i >= left; i--)
res[down][i] = cnt++;
down--;
if(up > down)
break;
for(int i = down; i >= up; i--)
res[i][left] = cnt++;
left++;
if(left > right)
break;
}
return res;
}
};

Leetcode59. Spiral Matrix II螺旋矩阵2的更多相关文章

  1. [Leetcode] spiral matrix 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 螺旋矩阵 II

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

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

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

  4. leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

    Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

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

  6. LeetCode 54. Spiral Matrix(螺旋矩阵)

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

  7. PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]

    1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...

  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(螺旋数组II)

    Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...

随机推荐

  1. 箭头函数报错:Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.

    解决:根目录新建babel.config.js加入如下内容 module.exports = { presets: [ "@babel/preset-env", "@ba ...

  2. System.Web.Mvc.HttpNotFoundResult.cs

    ylbtech-System.Web.Mvc.HttpNotFoundResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, ...

  3. Installer - win10安装及卸载SQL Server2008数据库

    一.数据库安装环境 操作系统:win10 SQL server:SQL server 2008 R2 二.全新数据库安装 1.安装扩展文件 双击安装文件,弹出如下窗体:                 ...

  4. <随便写>佛祖,哈哈!

    print(" _ooOoo_ ") print(" o8888888o ") print(" 88 . 88 ") print(" ...

  5. Linux常见问题解答--如何修复“tar:Exiting with failure status due to previous errors”

    问题: 当我用tar命令来创建一个压缩文件时,总在执行过程中失败,并且抛出一个错误说明"tar:由于前一个错误导致失败退出"("Exiting with failure ...

  6. Hibernate调用Oracle的存储过程

    众所周知,当过多的使用存储过程,触发器等 数据库方言相关的应用时,应用程序的移植性会变差,特别是在Hibernate中使用这些,简直是讽刺,但是当今中国又有哪家公司做项目会关心应用程序的移植性呢? 现 ...

  7. 2019-8-30-C#-如何在项目引用x86-x64的非托管代码

    title author date CreateTime categories C# 如何在项目引用x86 x64的非托管代码 lindexi 2019-08-30 08:53:52 +0800 20 ...

  8. mysql导出某张表的部分数据

    .使用into outfile '保存到操作系统的外部文件路径' mysql -uroot -p123456 -hhostname -P3306 select column_name_list fro ...

  9. 后缀自动机SAM

    某神犇:"初三还不会后缀自动机,那就退役吧!" 听到这句话后,我的内心是崩溃的. 我还年轻,我还不想退役--于是,我在后来,努力地学习后缀自动机. 终于,赶在初三开学前,我终于学会 ...

  10. Ionic 列表、文本 自动 换行

    1.采用row 布局的row-warp 来处理 <div class="item item-icon-right"> <span>图片相册</span ...