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. 【模板】树套树(线段树套Splay)

    如题,这是一个模板... #include <algorithm> #include <iostream> #include <cstring> #include ...

  2. lavarel 添加自定义辅助函数

    Laravel 提供了很多 辅助函数,有时候我们也需要创建自己的辅助函数. 必须 把所有的『自定义辅助函数』存放于 bootstrap 文件夹中. 并在 bootstrap/app.php 文件的最顶 ...

  3. python将excel数据写入数据库,或从库中读取出来

    首先介绍一下SQL数据库的一些基本操作: 1创建 2删除 3写入 4更新(修改) 5条件选择 有了以上基本操作,就可以建立并存储一个简单的数据库了. 放出python调用的代码: 此处是调用dos 操 ...

  4. 「新手必看」Python+Opencv实现摄像头调用RGB图像并转换成HSV模型

    在ROS机器人的应用开发中,调用摄像头进行机器视觉处理是比较常见的方法,现在把利用opencv和python语言实现摄像头调用并转换成HSV模型的方法分享出来,希望能对学习ROS机器人的新手们一点帮助 ...

  5. python-matplotlib-lec0

    直奔主题吧..以下是对matplotlib画图的简单讲解,代码已测试. win7 + pycharm + python 2.7 参考文档: http://old.sebug.net/paper/boo ...

  6. Nordic Collegiate Programming Contest 2015​ E. Entertainment Box

    Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fight ...

  7. 启动activity的标准的action常量及对应的字符串

  8. 光学字符识别OCR-5 文本切割

    经过前面文字定位得到单行的文本区域之后,我们就可以想办法将单行的文本切割为单个的字符了.因为第三步的模型是针对单个的字符建立的,因此这一步也是必须的. 均匀切割 基于方块汉字的假设,事实上最简单的切割 ...

  9. TOJ4168: Same Digits

    4168: Same Digits  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 11 ...

  10. java EE技术体系——CLF平台API开发注意事项(4)——API生命周期治理简单说明

    文档说明 截止日期:20170905,作者:何红霞,联系方式:QQ1028335395.邮箱:hehongxia626@163.com 综述 有幸加入到javaEE技术体系的研究与开发,也得益于大家的 ...