【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 ...
随机推荐
- 精通Python爬虫-03-狩猎大师
声明: 本系列文章原创于慕课网,作者秋名山车神,任何人不得以任何形式在不经作者允许的情况下,进行任何形式的印刷以及销售,转载需注明出处及此声明. 本系列文章更新至少每周一更,将涉及Python爬虫基础 ...
- Final冲刺贡献分
小组名称:Hello World! 项目名称:空天猎 组长:陈建宇 成员:刘成志.刘耀泽.刘淑霞.黄泽宇.方铭.贾男男 一.贡献分数规则: (1)基础分:5 , 4 ,4 , 3 , 2 ,2 ,1. ...
- [buaa-SE-2017]个人作业-week3
个人作业-week3:案例分析 分析产品:Bing词典 Part1:调研&评测 1.软件评测和Bug汇报 这次我选择Bing词典的原因是在于,首先我使用过的词典软件较多,平台涵盖PC端.网站. ...
- 第二阶段Sprint4
昨天:事实现保存到指定路径,并能够选择播放 今天:放弃改文件名,自动生成,实现暂停后继续录制 遇到的问题:不知道为什么实现不了,不然之前录制的视频就会丢失重新录
- 《Spring2之站立会议4》
<Spring2之站立会议4> 昨天,对主界面进行了设计,编写了主界面的代码,把文本输入框,显示框,发送,关闭两个按钮的功能实现了: 今天,接着对主界面进行代码的编写,实现了界面的美化,从 ...
- Mysql中实现递归查询
1.常规表字段 id,pid,lev,name 2.sql语句 DELIMITER // DROP PROCEDURE IF EXISTS Pro_GetColumnOrg//CREATE PROCE ...
- mac下使用marsedit写博客
在Windows习惯了使用Windows live write写好博客,然后发布上去,到了mac下,发现了一个很好的替代品 MarseEdit 下载安装后,打开出现如下解码,选择 I already ...
- cxVerticalGrid
cxVerticalGrid can't get values procedure TForm1.Button1Click(Sender: TObject); var i: Integer; lvNa ...
- php $_SERVER['HTTP_USER_AGENT']
//获取浏览器 function getBrowse() { global $_SERVER; $Agent = $_SERVER['HTTP_USER_AGENT']; $browseinfo='' ...
- Android 布局类控件
Android提供6种布局类的控件:LinearLayout.TableLayout.GridLayout.FrameLayout.RalativeLayout.AbsoluteLayout 网上搜到 ...