给定一个正整数 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. String方法之fromCharCode()和charCodeAt()

    1.fromCharCode fromCharCode() 可接受一个指定的 Unicode 值,然后返回一个字符串. 语法   我们可以根据 Unicode 来输出 "HELLO" ...

  2. 根据url提取网站域名的方法小结

    前言:最近使用到了他人总结的一个基础类库.查看了下源码,发现String帮助类的一个辅助方法不是很严谨,重构之. 1.原来程序的写法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  3. Java-MyBatis-MyBatis3-XML映射文件:insert, update 和 delete

    ylbtech-Java-MyBatis-MyBatis3-XML映射文件:insert, update 和 delete 1.返回顶部 1. insert, update 和 delete 数据变更 ...

  4. Android基础控件单选按钮RadioButton和Checkbox复选按钮的使用

    1.相关简介 RadioButton需要和RadioGroup结合使用,在RadioGroup设置布局方式! Checkbox是单独使用,本文为了方便放在了RadioGroup中! 2.简单使用 方法 ...

  5. 黑裙辉DS918+安装错误码21,安装教程 重装需要重新制作启动盘

    不然报错误码21    

  6. Struts2OGNL

    OGNL: 什么是OGNL  Object Graph Navigation Language 开源项目,取代页面中Java脚本,简化数据访问 和EL同属于表达式语言,但功能更为强大  OGNL在St ...

  7. 最小费用最大流——EK+SPFA

    终于把最小费用最大流学会了啊-- 各种奇奇怪怪的解释我已经看多了,但在某些大佬的指点下,我终于会了. 原来是个好水的东西. 最小费用最大流是什么? 不可能不知道网络流吧?如果不知道,自行百度去-- 费 ...

  8. Web API 接口说明文档

    1.采用 Web API Help Page 显示效果 2.swaggerui 创建文档接口 效果图 3.swagger ui 安装配置 nuget 安装 2.设置xml文件 3.配置根路径 预览sw ...

  9. Android实战技巧之四十一:制作自己的Android SDK

      标签: sdkandroid定制sdk 2015-09-21 18:05 11237人阅读 评论(2) 收藏 举报  分类: Android(260)  版权声明:本文为博主原创文章,未经博主允许 ...

  10. 学习Python笔记---变量和简单数据类型

    首先声明,这个是个人在自学的一些笔记,因为是小白,刚接触Python,之前也没有过类似的经验,所以很多东西对于其他人来说可能是小白级别的,写出来没有其他的意思就是自己整理然后记录一下,顺便分享出来,而 ...