LeetCode题目:Spiral Matrix II
原题地址:https://leetcode.com/problems/spiral-matrix-ii/
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
if( == n){
vector<vector<int>> coll;
return coll;
}
vector<vector<int>> coll(n, vector<int>(n, ));
int num = ;
for(int i = ; i < n - ; ++i){
for(int j = i; j < n - i; ++j)
coll[i][j] = num++;
for(int j = i + ; j < n - i; ++j)
coll[j][n - i -] = num++;
for(int j = n - i - ; j >= i; --j)
coll[n - i - ][j] = num++;
for(int j = n - i - ; j >= i + ; --j)
coll[j][i] = num++;
}
return coll;
}
};
LeetCode题目:Spiral Matrix II的更多相关文章
- [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】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(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 ...
随机推荐
- 构建maven动态web 工程
项目构建 总体参考: http://www.java2blog.com/2015/09/how-to-create-dynamic-web-project-using.html https://sta ...
- docker1.13新功能上要关注的点
如果要作单点端口映射,则需要结合constraint和label来定位具体的proxy机器吧. 如果不用这种模式,,ingress确实又太浪费集群端口了.. 纠结,,看看如何和compose v3作很 ...
- ubuntu14.04 安装 pyv8
1. $sudo pip install -v pyv8 Error: pip unicodedecodeerror ‘ascii’ codec can’t decode byte 0xe2 in p ...
- CF 1003D Coins and Queries【位运算/硬币值都为2的幂/贪心】
Polycarp has n coins, the value of the i-th coin is ai. It is guaranteed that all the values are int ...
- HDU 2280 Tetris Comes Back
状态压缩,$dp$,预处理. 设$dp[i][j]$为前$i-1$行填满,第$i$行为状态$j$的最小需要$1$种类的数量.预处理好每种状态能推出哪些状态,并且记录下所需花费就可以了. #pragma ...
- Proxy(2016山东省省赛C)(最短路)(spfa)
问题 C: Proxy 时间限制: 2 Sec 内存限制: 128 MB提交: 17 解决: 5[提交][状态][讨论版] 题目描述 Because of the GFW (Great Firew ...
- 【DFS】Codeforces Round #398 (Div. 2) C. Garland
设sum是所有灯泡的亮度之和 有两种情况: 一种是存在结点U和V,U是V的祖先,并且U的子树权值和为sum/3*2,且U不是根,且V的子树权值和为sum/3. 另一种是存在结点U和V,他们之间没有祖先 ...
- 【动态规划】Gym - 101147H - Commandos
裸dp,看代码. #include<cstdio> #include<algorithm> #include<cstring> using namespace st ...
- KEIL3中出现的字符不对齐的情况解决办法
写代码的时候我的keil3中会出现光标不对齐的情况,如下图: 看似光标在t后面,其实是在逗号后面,这是因为字体加粗导致的.解决办法: Edit->Configuration->colors ...
- Android获取屏幕的宽度和高度(dp)
public void getAndroiodScreenProperty() { WindowManager wm = (WindowManager) this.getSystemService(C ...