LeetCode OJ-- Spiral Matrix II
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的更多相关文章
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 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 ...
- [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 ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- LeetCode 59. Spiral Matrix II (螺旋矩阵之二)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- LeetCode 58 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [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 ...
- LeetCode题目:Spiral Matrix II
原题地址:https://leetcode.com/problems/spiral-matrix-ii/ class Solution { public: vector<vector<in ...
随机推荐
- [51Nod] 1218 最长递增子序列 V2
如何判断一个元素是否一定在LIS中?设f[i]为以ai结尾的LIS长度,g[i]为以ai开头的LIS长度,若f[i]+g[i]-1==总LIS,那么i就一定在LIS中出现 显然只出现一次的元素一定是必 ...
- phpExcel使用方法一
include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/Writer/Excel5 ...
- leetcode-8-pointer
void deleteNode(ListNode* node) { *node = *node->next; }
- MongoDB学习-->Spring Data Mongodb框架之Repository
application-dev.yml server: port: 8888 mongo: host: localhost port: 27017 timeout: 60000 db: mamabik ...
- Apache简易快速安装
转发出处:https://blog.csdn.net/qq_34804120/article/details/78862290 准备安装包 到https://www.apachelounge.com/ ...
- luogu3386 【模板】二分图匹配 匈牙利算法 hdu2063 过山车 dinic
luogu 匈牙利算法 #include <iostream> #include <cstring> #include <cstdio> using namespa ...
- SDOJ 3740 Graph
8.9 t3 [描述] 给你一个图,一共有 N 个点,2*N-2 条有向边. 边目录按两部分给出 1. 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点 到达. 2. ...
- Leetcode 475.供暖气
供暖气 冬季已经来临. 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖. 现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径. 所以,你的输入将会是房屋和供暖器 ...
- [uiautomator篇] 使用uiautomator需要导入uiautomator库
1 修改依赖文件:build/gradle( 是在app目录下)而不是和app同级目录的build/gradle androidTestCompile 'com.android.support.tes ...
- csa Round #70
Digit Holes Time limit: 1000 msMemory limit: 256 MB When writing digits, some of them are consider ...