https://oj.leetcode.com/problems/spiral-matrix-ii/

螺旋矩阵,和题目一一样的思路,这个是产生n*n 矩阵。

#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > ans;
if(n == )
return ans;
if(n ==)
{
vector<int> ansPiece;
ansPiece.push_back();
ans.push_back(ansPiece);
return ans;
}
ans.resize(n);
for(int i = ;i<n;i++)
ans[i].resize(n); int l1,l2,r2,p1;
l1 = ;
l2 = ;
r2 = n - ;
p1 = n -; int num = ;
while()
{
int i;
if(num>n*n)
break; for(i = l2; i <= r2; i++)
{
ans[l1][i] = num;
num++;
} if(l1+>p1)
break;
for(i = l1+;i<= p1;i++)
{
ans[i][r2] = num;
num++;
} if(r2-<l2)
break;
for(i = r2-;i>=l2;i--)
{
ans[p1][i] = num;
num++;
} if(p1-<l1+)
break;
for(i = p1-;i>=l1+;i--)
{
ans[i][l2] = num;
num++;
}
l1++;
l2++;
r2--;
p1--;
}
return ans;
}
};

LeetCode OJ-- 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 58 Spiral Matrix II

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

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

  10. LeetCode题目:Spiral Matrix II

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

随机推荐

  1. Mac配置gdb的一些问题

    1.Unable to find Mach task port for process-id 1527: (os/kern) failure (0x5).   (please check gdb is ...

  2. Qt概念和快捷键

    Qt概念和快捷键 Qt简介        1.Qt的由来和发展 Qt是一个1991年由Qt Company开发的跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程序,也可用于开发非GUI程 ...

  3. 基于axios的vue插件,让http请求更简单

    ajax-plus 基于axios 的 Vue 插件 如何使用 npm 模块引入 首先通过 npm 安装 ```npm install --save ajax-plus or yarn add aja ...

  4. 优酷项目之 ORM(数据库对象关系映射)代码重写

    前言: 我们在操作数据库时候一般都是通过sql代码来操作mysql数据库中相关数据,这就需要懂得sql语句,那么怎么样才能在不懂sql语句的情况下通过我们所学的python代码来实现对mysql数据库 ...

  5. Manjaro 添加国内源和安装搜狗输入法

    Manjaro 系统虽然比 Ubuntu 用着稳定,但有些小地方没有 Ubuntu 人性化,比如默认安装完的系统貌似没有中国的,Ubuntu 估计是用的人多,所以安装完后会根据所在地给你配置更新的源. ...

  6. jflash合并两个文件

    有时候需要将两个代码块烧写进入单片机的flash,可以使用合并的方法将两个文件合并为一个文件进行烧写,也可以分两次烧写,但要注意不要擦写不相关的存储空间. 打开J-FLASH,新建一个工程,然后fil ...

  7. linux学习-主机的细部权限规划:ACL 的使用

    传统的权限仅有三种身份 (owner, group, others) 搭配三种权限 (r,w,x) 而已,并没有办法单纯的针对某一个使用者或某一个群 组来设定特定的权限需求,此时就得要使用 ACL 这 ...

  8. poj 3107 Godfather(树的重心)

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7885   Accepted: 2786 Descrip ...

  9. HDU 3639 SCC Hawk-and-Chicken

    求SCC缩点,统计出每个SCC中的点的个数. 然后统计能到达u的最多的点的个数,可以反向建图,再dfs一遍统计出来. 最后说一下,有必要开一个标记数组,因为测试数据中有重边,结果无限WA. #incl ...

  10. 关于dispatch_sync死锁问题

    首先,我们来看下下面一个例子: 代码:(串行队列里同步线程嵌套)     NSLog(@"haha");     dispatch_queue_t queue = dispatch ...