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. ...
随机推荐
- 判断listview滑动方向的代码片段
mListView.setOnScrollListener(new OnScrollListener() { private int lastIndex = 0; @Override public v ...
- Kubernetes基础:Pod的详细介绍
本文的演练环境为基于Virtualbox搭建的Kubernetes集群,具体搭建步骤可以参考kubeadm安装kubernetes V1.11.1 集群 1. 基本概念 1.1 Pod是什么 Pod是 ...
- SVG.js 基础图形绘制整理(二)
一.折线 var draw = SVG('svg1').size(300, 300); //画折线 //使用字符串点 // var polyline=draw.polyline('0,0 100,50 ...
- Netty Associated -- Channel
A nexus to a network socket or a component which is capable of I/O operations such as read, write, c ...
- maven打包加时间戳
基于Maven的项目,发布时需要打包,如tar.gz.web项目打成war格式包.每次打包时希望自己加上时间戳,假如我的项目名是myproject,默认打包后名为myproject.war.而我希望的 ...
- OpenCV 脸部跟踪(3)
前面一篇文章我们生成了脸部特征的线性形状模型,本章来学习一下显示线性形状的代码. 线性模型类的结构如下: class shape_model { ...
- 最小二乘法多项式曲线拟合原理与实现 zz
概念 最小二乘法多项式曲线拟合,根据给定的m个点,并不要求这条曲线精确地经过这些点,而是曲线y=f(x)的近似曲线y= φ(x). 原理 [原理部分由个人根据互联网上的资料进行总结,希望对大家能有用] ...
- python 爬虫随机获取User-Agent
可以有两种方法: 1.随机生成 首先安装 pip install fake-useragent import random from fake_useragent import UserAgent d ...
- tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别
tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别 https://blog.csdn.net/u014365862/article/details/78238 ...
- ESXI安装时卡在loading ipmi_si_drv的解决方案
参考:http://x220ak.hatenablog.com/ 在这个界面按下shift+O,输入runweasel noipmiEnabled即可跳过loading ipmi_si_drv的加载