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. 互评Alpha版本—SkyHunter

    1.根据NABCD评论作品:   N(Need,需求):飞机大战题材的游戏对80,90后的人来说算是童年的记忆,可以在闲暇之余打开电脑玩一会儿.但是面向初中生,高中生的话这种PC小游戏可能不会那么适合 ...

  2. vue-router组件状态刷新消失的问题

    场景:vue-router实现的单页应用,登录页调用登录接口后,服务器返回用户信息,然后通过router.push({name: 'index', params: res.data})跳转到主页,并在 ...

  3. sampleFactory(女娲造人)

    使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,如果传入参数R,则返回一个Robot对象. package c ...

  4. 安卓开发神器vysor+adb wifi

    准备: 1.vysor需要FQ从google应用商店下载,装在google上,目前知道的免费的vysor的作用是电脑显示手机屏幕并且能操控手机. 步骤:FQ后就能下载了,FQ方法不赘述.

  5. 关于map和hashmap

    今天做的程序猿那题 在公司里面,程序猿经常有一堆todolist要做,而这些todolist是产品经理分配给他们的.但是当程序员遇到不懂技术的产品狗时,就悲剧了.产品经理经常修改他们的todolist ...

  6. 四则运算App--大总结(已完成)

    1. 贡献分分配(20分) 欧泽波:14分,Android的学习,代码的编写,等等 杨洁华:1分,提供学习资料,框架的设计等等 赵泽嘉:3分,提供学习资料,框架的设计等等 林扬滨:2分,提供学习资料, ...

  7. BETA-7

    前言 我们居然又冲刺了·七 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 过去两天完成了哪些任务 经过分析发现,为提升速率估测的可靠性,目前最具可改造性的参数为帧间间隔,调用参 ...

  8. angularJS1笔记-(15)-自定义指令(accordion伸缩菜单原始实现)

    index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  9. js访问对象属性的两种方法

    var obj={name:'fuuf',age:19} 第一种,用.访问 obj.name 第二种 用[]访问 obj['name']  //此时name是字符串,要加引号 注意事项 使用第二种方法 ...

  10. 【百度】大型网站的HTTPS实践(二)——HTTPS加密算法介绍

    大型网站的HTTPS实践(二)——HTTPS加密算法介绍 原创 网络通信/物联网 作者:AIOps智能运维 时间:2018-11-09 15:09:43  358  0 前言 在上一篇文章中,我们简要 ...