spiral-matrix-ii &i 生成顺时针序列
I:
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return[1,2,3,6,9,8,7,4,5].
按顺时针取序列,因为序列不一定是正矩阵,所以需要每取完一个方向要当即--或++,并做判断是否需要再取下一个方向。
当取完left->right,需要top++表明上一行已取完,top->bottom需right--,right->left需先判断top<=bottom避免只有单行重复取了上行,而后bottom++,bottom->top需先判断left<=right避免重复取右列,而后left++;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
int m=matrix.size();
vector<int> res;
if(m==)
return res;
int n=matrix[].size();
int left=,right=n-,top=,bottom=m-;
int i=;
while(left<=right&&top<=bottom)
{
for(i=left;i<=right;++i)
res.push_back(matrix[top][i]);
top++;
for(i=top;i<=bottom;++i)
res.push_back(matrix[i][right]);
right--;
if(top<=bottom){
for(i=right;i>=left;--i)
res.push_back(matrix[bottom][i]);
}
bottom--;
if(left<=right){
for(i=bottom;i>=top;--i)
res.push_back(matrix[i][left]);
}
left++;
}
return res;
}
};
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 ]
]
注意左->右可遍历全,剩余两个方向遍历时需+1,最后一个方向掐头去尾避免重复遍历
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int>> result(n,vector<int>(n));
if(n==)
return result;
int step = ;
int left = ; int right = n-; int top = ; int bottom = n-;
while(left<=right && top<=bottom)
{
// 左->右
for(int i=left;i<=right;i++) {
result[top][i] = step;
step++;
}
//上->下
for(int i=top+;i<=bottom;i++) {
result[i][right] = step;
step++;
}
//右->左
for(int i=right-;i>=left;i--) {
result[bottom][i] = step;
step++;
}
//下->上
for(int i=bottom-;i>top;i--) {
result[i][left] = step;
step++;
}
left++; right--; top++; bottom--;
}
return result;
}
};
spiral-matrix-ii &i 生成顺时针序列的更多相关文章
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
- C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 59. Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
随机推荐
- jdgui反编译+javac编译=无源文件改动代码
首先我们要知道,打包好的Java程序中都是编译好的字节码文件(*.class).这些class文件会在执行的时候被载入到JVM中. 若想替换掉某一个类,那么仅仅须要将该类的源代码又一次编译然后再替换之 ...
- 5G与TCP/IP
众所周知,4G LTE全面IP化,上层传输走的是TCP/IP协议(如下图). ▲LTE用户面协议构架 但是,我们熟悉的这个构架并没有发挥出移动网络的全部潜力,甚至阻碍了未来网络的发展. 1 TCP/ ...
- 数学图形之贝塞尔(Bézier)曲面
前面章节中讲了贝塞尔(Bézier)曲线,而贝塞尔曲面是对其多一个维度的扩展.其公式依然是曲线的公式: . 而之所以由曲线变成曲面,是将顶点横向连了再纵向连. 很多计算机图形学的教程都会有贝塞尔曲面的 ...
- [数学] 将长为L的木棒随机折成3段,则3段构成三角形的概率
1.题目 如题,将长为L的木棒随机折成3段,则3段构成三角形的概率是多少? 2.解析 这个与其说是编程题目,倒不如说是个数学题目.可能是你没有见过,或者没有过这种解题思想.(还是那句,一回生两回熟,类 ...
- linux和windows时间同步问题(UTC&localtime)
Linux使用 UTC,但是windows默认使用localtime.解决的办法如下(重启后生效). 进入windows使用regedit写入DWORD值(设置成十六进制"1"): ...
- 超简单!80行代码实现Google日历(拖放、移动、AJAX)
介绍 本实例介绍使用DayPilot Lite for ASP.NET MVC library 类来实现类google日历效果. 在线实例 天视图 星期视图 拖放调整 拖放移动 行代码来实现a ...
- 解剖android中的闹钟app 一
首先,看一看android市场上有哪些主流的闹钟app了,我们来进行一个简单的评测: 一.正点闹钟 这是一款源自金山技术的闹钟app,其主力创始团队都是来自于金山,其装机量,下载量都是排名第一.老样子 ...
- 艾伦AI研究院发布AllenNLP:基于PyTorch的NLP工具包
https://www.jiqizhixin.com/articles/2017-09-09-5 AllenNLP 可以让你轻松地设计和评估几乎所有 NLP 问题上最新的深度学习模型,并同基础设施一起 ...
- PHPExcel合并与拆分单元格
$objPHPExcel; $filepath="c:\temp.xlsx"; try { $objReader = PHPExcel_IOFactory::createRea ...
- Chrome DevTools — Network
Chrome DevTools — Network https://segmentfault.com/a/1190000008407729 chrome DevTools的各种使用: 在dev too ...