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

    One of the most common birth defects throughout the world is a cleft lip. Babies born with a cleft l ...

  2. 公式中表达单个双引号【"】和空值【""】的方法及说明

    http://club.excelhome.net/thread-661904-1-1.html 有人问为什么不用三个双引号"""来表示单个双引号["]呢,如果 ...

  3. SpringDataJPA最佳实践(一)简介

    在团队中使用SpringDataJPA有一段时间了,也踩过一些坑,这里记录一下学习历程. 1.简介 Spring Data是什么 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架 ...

  4. winform messagebox 统一

    vb.net 里面有两种messagebox,一种是vb语言提供的msgbox,另一种是.net framework 提供的messagebox.在此统一使用messagebox. Warning,提 ...

  5. ambassador 学习四 grpc 处理

    实际上都是envoy 的功劳 基本环境安装参考相关文档即可 参考demo proto code syntax = "proto3"; option java_multiple_fi ...

  6. Chrome 的应用功能越来越强大

    Chrome 的应用功能越来越强大 升级到 版本 70.0.3538.77 最早的时候是看到 http 显示地址,现在可以在快捷应用中显示扩展了,还可以看到显示的站点. 现在越来越强大了.

  7. 实用的IP地址处理模块IPy

    https://www.cnblogs.com/cherishry/p/5916935.html IPy安装 pip install IPy IP地址.网段的基本处理 IPy模块包含IP类,使用它可以 ...

  8. GPU 服务器环境安装中一些基础note

    GPU 服务器环境安装中一些基础note GPU 服务器: 添加组,用户,并为之新建主目录. c302@c302-dl:~$ sudo addgroup testgroup Adding group ...

  9. Python 迭代对象、迭代器、生成器

    原文出处: liuzhijun 本文源自RQ作者的一篇博文,原文是Iterables vs. Iterators vs. Generators,俺写的这篇文章是按照自己的理解做的参考翻译,算不上是原文 ...

  10. python 书籍推荐 三

    主要先学习<python语言入门>学完后,研究<征服python>Python简明教程(A Byte of Python) 此书讲解简洁易懂,适合初学者 剖析Python源代码 ...