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

思路:创建函数互相递归调用,函数的参数要包括方向

class Solution {
public:
vector<int> spiralOrder(vector<vector<int>> &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
result.clear();
if(matrix.empty()) return result; leftPos = ;
rightPos = matrix[].size()-;
topPos = ;
bottomPos = matrix.size()-;
goWider(matrix, true);
return result;
}
void goWider(vector<vector<int>> &matrix, bool direct)
{
if(direct)
{
for(int i = leftPos; i<= rightPos; i++)
{
result.push_back(matrix[topPos][i]);
}
topPos++;
if(topPos > bottomPos) return;
goDeeper(matrix, true);
}
else
{
for(int i = rightPos; i>= leftPos; i--)
{
result.push_back(matrix[bottomPos][i]);
}
bottomPos--;
if(topPos > bottomPos) return;
goDeeper(matrix, false);
}
}
void goDeeper(vector<vector<int>> &matrix, bool direct)
{
if(direct)
{
for(int i = topPos; i<= bottomPos; i++)
{
result.push_back(matrix[i][rightPos]);
}
rightPos--;
if(leftPos > rightPos) return;
goWider(matrix, false);
}
else
{
for(int i = bottomPos; i>= topPos; i--)
{
result.push_back(matrix[i][leftPos]);
}
leftPos++;
if(leftPos > rightPos) return;
goWider(matrix, true);
}
}
private:
vector<int> result;
int leftPos;
int rightPos;
int topPos;
int bottomPos;
};

54. Spiral Matrix (Graph)的更多相关文章

  1. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  2. [Leetcode][Python]54: Spiral Matrix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...

  3. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  4. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  5. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  6. [array] leetcode - 54. Spiral Matrix - Medium

    leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...

  7. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  8. 54. Spiral Matrix

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

  9. LeetCode OJ 54. Spiral Matrix

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

随机推荐

  1. 201621123010《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词. 继承extends.多态.抽象类 超级父类Object类.父类.子类. 覆盖Override 初始化块 识别类 重载 1.2 ...

  2. L175 Endorestiform Nucleus: Scientist Just Discovered a New Part of the Human Brain

    The newly named Endorestiform Nucleus sits in the inferior cerebellar小脑 peduncle, at the junction be ...

  3. Vim技能修炼教程(17) - 编译自己的Vim

    编译自己的Vim 前面我们已经对Vim有比较丰富的了解了.我们也知道Vim有很多编译时的选项,很多功能依赖于这些编译选项.其中最重要的就是脚本语言的支持,很多发行版本是不全的.为了支持我们所需要的功能 ...

  4. centos7 中将执行文件python链接为python3后 如何保证 yum 功能不受影响

    1.  查看  /usr/bin  中  python 执行文件的 链接情况 2.  设置   python  命令的可执行文件  链接为    python3 3.  此时 , yum  文件中的p ...

  5. 设计一个栈,设计一个max()函数,求当前栈中的最大元素

    #include <iostream> using namespace std; #define MAXSIZE 256 typedef struct stack { int top; i ...

  6. MySQL binlog日志优化

    mysql中日志类型有慢查询日志,二进制日志,错误日志,默认情况下,系统只打开错误日志,因为开启日志会产生较大的IO性能消耗.   一般情况下,生成系统中很少打开二进制日志(bin log),bin ...

  7. 使用ffmpeg步骤(转)

    av_register_all();//初始化ffmpeg库,如果系统里面的ffmpeg没配置好这里会出错  if (isNetwork) {      //需要播放网络视频      avforma ...

  8. Web验证方式(3)--OAuth 2.0协议

    介绍 OAuth协议是用来解决第三方应用程序访问Http Service的时候的认证问题.举个例子:某视频网站支持用户通过微信登陆,然后获取用户在微信上的图像信息. 在这个场景里 微信充当的就是Htt ...

  9. Intellij Idea上Spring Boot编译报错:Error:(3, 32) java: 程序包org.springframework.boot不存在

    很尴尬,为了使用Spring Boot的Initializr,特意下了个Intellij Idea,刚按提示新建一个Spring Boot的Maven项目后,就出现红叉叉了.因为IDE是新的,开始是M ...

  10. C#综合揭秘——细说事务

    引言 其实事务在数据层.服务层.业务逻辑层多处地方都会使用到,在本篇文章将会为大家一一细说. 其中前面四节是事务的基础,后面的三节是事务的重点,对事务有基础的朋友可以跳过前面四节. 文章有错漏的地方欢 ...