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 ...
随机推荐
- MyEclipse的破解代码,适用各个版本
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public ...
- 二分+Dfs【p1902】刺杀大使
题目描述--->p1902 刺杀大使 题意概括: 找一条路径,使得从第1行到第n行路径的最大值最小. 分析: 题目概括出来,很容易想到二分. 求最大值最小,因此我们可以对最大伤害值进行二分. 如 ...
- 更新archlinux
有个上网本,虽然配置很差,但是安装的是arch,这不长时间不滚动更新出问题了, :: Proceed with installation? [Y/n] (/) checking keys % (/) ...
- 【状压DP】poj2686 Traveling by Stagecoach
状压DP裸题,将({当前车票集合},当前顶点)这样一个二元组当成状态,然后 边权/马匹 当成边长,跑最短路或者DAG上的DP即可. #include<cstdio> #include< ...
- HTML5 Boilerplate笔记(2)(转)
最近看到了HTML5 Boilerplate模版,系统的学习与了解了一下.在各种CSS库.JS框架层出不穷的今天,能看到这么好的HTML模版,感觉甚爽.写篇博客,推荐给大家使用. 一:HTML5 ...
- iOS中设置backBarButtonItem的title和action
一. 设置title 在需要显示该返回键的前一个Controller中设置: 1: navigationItem.backBarButtonItem = UIBarButtonItem(title: ...
- web 中加载配置文件
1.web.xml中配置 <!-- 加载配置文件 --> <listener> <description>ServletContextListen ...
- IntelliJ IDEA字符串常量长度太长的问题解决:constant string too long
Java compiler下的Use compiler为Eclipse:
- Word里如何打出带有上下横杠的大写字母i
换成新罗马就行了.
- Bootstrap标签Tabs
<!--标签--> <ul class="nav nav-tabs" role="tablist"> <li class=&quo ...