【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 ...
随机推荐
- 冲刺One之站立会议3 /2015-5-16
2015-5-16 今天我们主要完成一部分服务器端的内容,因为只有服务器端完成了主要功能其他的部分才可以测试有没有成功实现目标.具体包括服务器登陆时需要的端口号.启动时间.服务器状态的显示.在线人数等 ...
- BNUOJ 52308 We don't wanna work! set模拟
题目链接: https://acm.bnu.edu.cn/v3/problem_show.php?pid=52308 We don't wanna work! Time Limit: 60000msM ...
- Beta冲刺 (6/7)
队名:天机组 组员1友林 228(组长) 今日完成:修改代码 明天计划:封装代码 剩余任务:优化网络通讯机制 主要困难:暂无 收获及疑问:暂无 组员2方宜 225 今日完成:优化了一部分活动,调整了界 ...
- keil51下使用sprintf问题
测试环境:keil c51 + STC89C52说明: 1.keil的不定参数只有15个字节也就是说sizeof(...) 加起来总共不能超过15字节,否则会出错 2.当不定参数中有常数时,你也会得不 ...
- grunt入门讲解4:如何创建task(任务)
每当运行Grunt时, 你可以为其指定一个或多个任务, 这些任务用于告诉Grunt你想要它做什么事情. 如果你没有指定一个任务,并且你已经定义一个名为 "default" 的任务, ...
- .NET 类库研究必备参考 扣丁格鲁
.NET 类库的强大让我们很轻松的解决常见问题,作为一个好专研的程序员,为了更上一层楼,研究CLR的基础类库实现是快速稳定的捷径. 一般场景下,采用 Reflector可以反射出.NET 的部分实现出 ...
- 确保你想要修改的char*是可以修改的
void change(char *source) { source[] = 'D'; cout<<source<<endl; } 考虑一下,你有这么一个函数change它的作 ...
- 绿色计算大赛决赛 第二阶段 消息传递(斯坦纳树 状压dp+spfa)
传送门 Description 作为公司老板的你手下有N个员工,其中有M个特殊员工.现在,你有一个消息需要传递给你的特殊员工.因为你的公司业务非常紧张,所以你和员工之间以及员工之间传递消息会造成损失. ...
- java 循环时候当达到这个类型的极值时 会停止输出
- java 对象的解释过程