Spiral Matrix

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

题意很简单,就是顺时针螺旋打印矩阵。思路也很简单,就是把元素分别从左向右、上到下、右到左和下到上螺旋保存到一个数组中。但是需要注意细节,特别是边界条件判断。

 #include <iostream>
#include <vector> using namespace std;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
vector<int> ans;
if(matrix.empty())
return ans;
int begin_row = , end_row = matrix[].size() - ;//begin_row is the row number, end_row is the remaind size of each row
int begin_col = , end_col = matrix.size() - ;//begin_col is the col number,end_col is the remaind size of each col
while(true){
for(int i = begin_row; i <= end_row; ++i)//left to right
ans.push_back(matrix[begin_row][i]);
if(++begin_col > end_col) break; for(int i = begin_col; i <= end_col; ++i)//up to down
ans.push_back(matrix[i][end_row]);
if(begin_row > --end_row) break; for(int i = end_row; i >= begin_row; --i)//right to left
ans.push_back(matrix[end_col][i]);
if(begin_col > --end_col) break; for(int i = end_col; i >= begin_col; --i)//bottom to up
ans.push_back(matrix[i][begin_row]);
if(++begin_row > end_row) break;
}
return ans;
}
}; int main()
{
Solution s;
vector<vector<int> > matrix;
vector<int> v;
for(int i = ; i <=; i++){
v.push_back(i);
if(i % == ){
matrix.push_back(v);
v.clear();
}
}
vector<int> ans = s.spiralOrder(matrix);
for(int i = ; i < ans.size(); ++i)
cout<< ans[i] <<endl;
return ;
}

【leetcode】 Spiral Matrix的更多相关文章

  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. 【leetcode】Spiral Matrix II (middle)

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

  3. 【leetcode】Spiral Matrix

    题目概要: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spi ...

  4. 【leetcode】Spiral Matrix(middle)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  5. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Set Matrix Zeroes 解题报告

    今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...

  8. 【数组】Spiral Matrix II

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

  9. 【数组】Spiral Matrix

    题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spira ...

随机推荐

  1. 看oracle的sid

    ps -ef|grep pmon 可以从进程名字里看到 也可以通过 sqlplus / as sysdbashow parameter instance_name

  2. PSP Daily新增功能说明书

    1.选择输入类别时可以记录原来的输入,支持用户选择记录清空功能 2.添加“恢复最近”button,点击这个按钮可以跳出一个页面显示最近的excel记录,用户可以通过勾选相应的excel文件名,恢复选中 ...

  3. 解决Cygwin编译cocos2dx 遇到的 error: 'UINT64_C' was not declared in this scope 问题

    环境工具:Win10.VS2013.cocos2d-x-2.2.6.Cygwin.ADT 问题来源:写了一个小游戏,VS2013上运行成功,就尝试着打包apk,项目导入到ADT里面,添加了cocos2 ...

  4. Task 6.2站立会议三

    今天我完成了软件的主要聊天界面的视频通话和语音通话的部分功能,过程中遇到很多不会的知识.因为使用的是C#,虽然很容易上手但是还会存在很多不懂得内容.

  5. Beta后续感想/吐槽

    感想 磨人的软工实践终于结束了 艰难的度过了一学期,还是写点什么纪念一下吧. 大一大二的时候就听说软工实践是魔鬼般的锻炼,于是当年不知天高地厚的我是很期待的,终于,我大三了. 后来,我长大了. alp ...

  6. 浅学CSS

    CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明: 选择器通常是您需要改变样式的 HTML 元素. 每条声明由一个属性和一个值组成. 属性(property)是您希望设置的样式属性(sty ...

  7. C51中的关键字data,idata,xdata,pdata,bdata

    写在最前面的话:官方网站的解答是最可信的.英语不错的必看.http://www.keil.com/support/man/docs/c51/c51_le_memtypes.htm 下面转载几篇中文的, ...

  8. Rsyslog日志服务搭建

    rsyslog是比syslog功能更强大的日志记录系统,可以将日志输出到文件,数据库和其它程序.Centos 6.x默认的rsyslog版本是5.x. 网上关于rsyslog的安装配置文档倒是不少,但 ...

  9. display:flex 布局教程,弹性布局!

    display:flex 布局教程 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. ...

  10. [PPT] PPT 录制视频功能.

    1. 需要PPT 里面增加进截图, 发现还不如 直接插入视频合理 本来想了一种方式是 使用 screen to gif 的工具 生成gif 来处理. 后来 发现ppt 里面自带一个 屏幕录制功能. 2 ...