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 生成顺时针序列的更多相关文章

  1. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  2. 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 ...

  3. Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  4. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  5. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  6. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  7. 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 ...

  8. C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...

  9. 【LeetCode】59. Spiral Matrix II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

  10. 59. Spiral Matrix II

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

随机推荐

  1. 超能英雄第一至四季/全集Heroes迅雷下载

    本季第一.二.三.四季 Heroes Season (2006-2009) 看点:<Heroes>是NBC电视台于2006年九月开播的最新科幻电视剧.Heroes(中文剧名为“英雄”或“天 ...

  2. http抓包以及网速限定

    由于我是MAC, 举荐一个Charles工具 限速选择在:可以打开Proxy –> Throttle Setting 设置. 附多篇介绍:http://www.36ria.com/6278 ht ...

  3. Android之一种很有趣的界面跳动提示动画

    上一个效果图: ==================================== 先上布局: <RelativeLayout xmlns:android="http://sch ...

  4. lucene4之后的近实时搜索实现

    好久没干这块东西了,近几天须要做这个.所以又一次学了一下.首先很感谢孔浩老师,没孔浩老师的视频我也不会进入lucene的殿堂. 老师当时讲的实时搜索还是NRTManager,如今已经都变了,这个类已经 ...

  5. [Hook] 免root,自己进程内,startActivity hook的几种姿势

    首先关于startActivity 我们平时会经常使用到 在activity内 直接startActivity(intent) 其实这里还有一种start方式 我们可能没怎么用过 getApplica ...

  6. C#与Java的语法差异

    C#与Java的语法差异C与Java的语法差异前言程序结构基本语法数据类型字符串变量与常量运算符判断语句循环语句访问权限方法数组结构枚举类继承多态运算符重载接口命名空间预处理器指令正则表达式异常IO泛 ...

  7. 虚拟私有云(Virtual Private Cloud,专有网络)配置方式总结

    虚拟私有云 虚拟私有云(Virtual Private Cloud)是用户在云上申请的隔离的.私密的虚拟网络环境.用户可以自由配置VPC内的IP地址段.子网.安全组等子服务,也可以申请弹性带宽和弹性公 ...

  8. you have mixed tabs and spaces fix this

    http://blog.csdn.net/tonyyan19781/article/details/60882443 Vs2013 IDE下,编辑C++的工程源码,在打开文件的时候,会出现 " ...

  9. 【Android归纳】开发中应该注意的事项

    1.子线程中不能更新界面,更新界面必须在主线程中进行 2.Fragment注意的事项: a)  Activity调用Fragment中的方法 b)  Thread或者Handler调用Fragment ...

  10. Excel VBA 从一个工作簿查找另一个一个工作簿中的一些内容复制到另外一个工作簿

    帮朋友来写个Excel VBA 以前写过ASP,所以对vb略微熟悉,但VBA 没有仔细研究过. 以前只研究过 vba 写一个 计算个人所得税的程序. 这次写的功能也算是简单,但也耗费了两天的功夫. 需 ...