[Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order.
For example,
Given n =3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
题意:给定整数n,以螺旋的方式形成一个n×n个矩阵。
思路:这题的思路和spiral matrix 一样。还是按照螺旋的方式去赋值,由外到内的一层层赋值。这两题的区别在于,本题中,不可能剩下1×k或者k×1的区域没有赋值,因为这个是一个正方形,所以在讨论最初和最后情况时,只需要一个if条件判断即可。还有一点要注意的是,返回值一定要先赋值,不然用下标访问不存在的地方会报错。代码如下:
class Solution {
public:
vector<vector<int> > generateMatrix(int n)
{
vector<vector<int>> matrix(n,vector<int>(n,));
if(n==) return matrix;
int left=,right=n-;
int up=,down=n-;
int num=;
while(right>=left && down>=up)
{
if(right==left)
{
matrix[up][right]=num;
return matrix;
}
for(int i=left;i<right;++i)
{
matrix[up][i]=num;
num++;
}
for(int i=up;i<down;++i)
{
matrix[i][right]=num;
num++;
}
for(int i=right;i>left;i--)
{
matrix[down][i]=num;
num++;
}
for(int i=down;i>up;i--)
{
matrix[i][left]=num;
num++;
}
left++;
right--;
up++;
down--;
}
return matrix;
}
};
方法二:也可以使用旋转图片中的思想,只不过比较难想一些。代码如下:
class Solution
{
public:
vector<vector<int>> generateMatrix(int n)
{
vector<vector<int>> matrix(n,vector<int>(n,)); //一定要给matrix给赋值。
// 由外层到里,一层考虑
if(n==)
{
matrix[][]=;
return matrix;
}
int temp=;
//层数
for(int layer=;layer<n/;++layer)
{
int first=layer;
int last=n--layer;
int num=last-first;
//每一层上,每一行、列,找出相应规律,都从顶点赋值
for(int i=first;i<last;++i)
{
int offset=i-first;
matrix[first][i]=temp+i;
matrix[i][last]=temp+num+i;
matrix[last][last-offset]=temp+*num+i;
matrix[last-offset][first]=temp+*num+i;
}
temp+=*num-;
}
//当n为奇数时,最中心的一个为n^2
if(n%==) matrix[n/][n/]=n*n;
return matrix;
}
};
[Leetcode] spiral matrix ii 螺旋矩阵的更多相关文章
- [LeetCode] 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 ...
- Leetcode59. Spiral Matrix II螺旋矩阵2
给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...
- leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?
Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- LeetCode 54. Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- [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 ...
随机推荐
- MongoDB在单机上搭建分片副本集群(windows)
------------------------------1.安装MongoDB...... ------------------------------2.准备好文件夹 --config:配置文件 ...
- java基础day05---界面
java基础day05---界面 1.GUI:图形用户界面(Graphics User Interface) 开发工具包AWT抽象窗口把工具箱===>swing 解决了awt存在的lcd问题== ...
- R语言学习笔记(十七):data.table包中melt与dcast函数的使用
melt函数可以将宽数据转化为长数据 dcast函数可以将长数据转化为宽数据 > DT = fread("melt_default.csv") > DT family_ ...
- R语言绘图:在地图上绘制热力图
使用ggplot2在地图上绘制热力图 ######*****绘制热力图代码*****####### interval <- seq(0, 150000, 25000)[-2] #设置价格区间 n ...
- 最短路径算法 1.Floyed-Warshall算法
这几周开始正式系统学习图论,新学期开始新的记录.由于二模和生物地理两门高考的临近,时间比较仓促,所以暂时跳过图论的(一)和(二),即图的储存和遍历.从最短路径算法学起,首先要学习的是Floyed-Wa ...
- 1754-I Hate It 线段树(单点替换,区间最值)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 四大IO抽象类
四大IO抽象类 InputStream/OutputStream和Reader/writer类是所有IO流类的抽象父类,我们有必要简单了解一下这个四个抽象类的作用.然后,通过它们具体的子类熟悉相 ...
- autofac无法解析一例
在asp.net mvc分项目开发中,如果类库位于其他的项目中,则必须在global中对其他项目的类库进行注册,否则会报“ None of the constructors found with 'A ...
- java设计模式大全 Design pattern samples in Java(最经典最全的资料)
java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...
- Android OpenStreetMap(OSM) 使用 osmbonuspack 进行导航
关于OpenStreetMap的介绍,国内还是很少,csdn上面有一篇,写的不错,我也就不再做重复的事情了. 这里贴出链接地址:http://blog.csdn.net/mad1989/article ...