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. ...
随机推荐
- Eclipse with ADT的安装和配置
我们从安卓官方网站(https://developer.android.com/sdk/index.html#download)下载下来的eclipse是捆绑好了ADT的,所以不用自己安装插件,十分方 ...
- JAVAWEB开发之HttpServletResponse和HttpServletRequest详解(下)(各种乱码、验证码、重定向和转发)
HttpServletRequest获取请求头信息 (1)获取客户机请求头 String getHeader(String name) Enumeration<String> getHe ...
- tensorflow报错屏蔽的方法
之前是报这样的错: OpKernel ('op: "BestSplits" device_type: "CPU"') for unknown op: BestS ...
- 第四章 logstash - type与tags属性
参考:http://kibana.logstash.es/content/logstash/plugins/input/stdin.html 最常用法: input { stdin { tags =& ...
- C++ 模板学习 函数模板、类模板、迭代器模板
使用模板能够极大到使得代码可重用. 记录一下,方便后续使用. 1. 函数模板,支持多种类型参数 #include <stdio.h> #include <math.h> //函 ...
- Windbg简明教程(转)
Windbg是Microsoft公司免费调试器调试集合中的GUI的调试器,支持Source和Assembly两种模式的调试.Windbg不仅可以调试应用程序,还可以进行Kernel Debug(新版本 ...
- ie7 z-index 失效问题
解决办法: 父级元素加上position:relative;并设置z-index. 父级元素的z-index优先,其中包含的元素的z-index是相对于父级元素的index. <div styl ...
- C# Win32控制台应用程序忽略 Ctrl + C,阻止程序退出
C# Win32控制台应用程序忽略 Ctrl + C,阻止程序退出,这里使用到了Windows API SetConsoleCtrlHandler函数 注意:在VS中调试执行时,在处理程序例程中设置断 ...
- 斯坦福深度学习与nlp第四讲词窗口分类和神经网络
http://www.52nlp.cn/%E6%96%AF%E5%9D%A6%E7%A6%8F%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E4%B8%8Enlp%E7%A ...
- MarkDownPad Pro 支持github格式的markdown语法
1. http://blog.csdn.net/xiaohei5188/article/details/43964451