【leetcode】 Spiral Matrix
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的更多相关文章
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【leetcode】Spiral Matrix
题目概要: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spi ...
- 【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 ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Set Matrix Zeroes 解题报告
今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...
- 【数组】Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【数组】Spiral Matrix
题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spira ...
随机推荐
- Notes of Daily Scrum Meeting(11.13)
Notes of Daily Scrum Meeting(11.13) 今天邹欣老师给我们讲课大家还是很有收获的,大家课堂的参与度确实有了很大的提升,而且邹欣老师关于项目Scrum Meeting报告 ...
- 20172332 2017-2018-2 《程序设计与数据结构》Java哈夫曼编码实验--哈夫曼树的建立,编码与解码
20172332 2017-2018-2 <程序设计与数据结构>Java哈夫曼编码实验--哈夫曼树的建立,编码与解码 哈夫曼树 1.路径和路径长度 在一棵树中,从一个结点往下可以达到的孩子 ...
- 听说 —— beta冲刺总结
听说 -- beta冲刺总结 beta冲刺成员名单 姓名 学号 负责方向 个人主页 周龙荣 031402543 前端页面.跳转 http://www.cnblogs.com/ZHOULR/ 李家鹏 0 ...
- Gradle入门(5):创建二进制发布版本
在创建了一个实用的应用程序之后,我们可能想将其与他人分享.其中一种方式就是创建一个可以从网站上下载的二进制文件. 这篇教程描述了如何创建一个二进制发布版本,满足以下需求: 二进制发布一定不能使用所谓的 ...
- J2EE 13种技术规范
J2EE平台由一整套服务(种技术规范进行简单的描述(限于篇幅,这里只能进行简单的描述): 1.JDBC(Java Database Connectivity): JDBC API为访问不同的数据 ...
- 关于51精确延时及keil仿真延时时间
转自:http://blog.sina.com.cn/s/blog_980e19e00101b5dh.html 有时候需要精确的延时,比如18B20温度传感器对时序要求非常严格,必须精确到微秒级别 一 ...
- 队列java实现
队列是一种线性数据结构,是一种运算受限的线性表,只允许在队尾插入,在队头删除.运算规则是先进先出.恰好和栈相反.栈是先进后出.因为栈只在栈顶做删除和插入. 队列按照存储结构可以分为顺序队列和链式队列. ...
- linux shell 执行命令顺序
1.shell命令搜索顺序 在linux shell 中输入一个命令,如果有多个同名指令,shell需要按照一定规则去取优先级高的一个执行,shell命令的搜索顺序为: 1.别名,使用alias创建的 ...
- 015 反射中的 Class.forName() 与 ClassLoader.loadClass() 的区别
作者:nnngu GitHub:https://github.com/nnngu 博客园:http://www.cnblogs.com/nnngu 简书:https://www.jianshu.com ...
- elasticsearch使用More like this实现基于内容的推荐
基于内容的推荐通常是给定一篇文档信息,然后给用户推荐与该文档相识的文档.Lucene的api中有实现查询文章相似度的接口,叫MoreLikeThis.Elasticsearch封装了该接口,通过Ela ...