54. Spiral Matrix (Graph)
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)的更多相关文章
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- [Leetcode][Python]54: Spiral Matrix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- 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 ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- [array] leetcode - 54. Spiral Matrix - Medium
leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- 54. Spiral Matrix
题目: Given a matrix of m x n elements (m rows, ncolumns), return all elements of the matrix in spiral ...
- 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 ...
随机推荐
- 201621123006 《Java程序设计》第8周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下: 由源代码可 ...
- SpringDataJPA最佳实践(一)简介
在团队中使用SpringDataJPA有一段时间了,也踩过一些坑,这里记录一下学习历程. 1.简介 Spring Data是什么 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架 ...
- I.MX6 PWM buzzer driver hacking with Demo test
/***************************************************************************** * I.MX6 PWM buzzer dr ...
- BZOJ3296:Learning Languages(简单并查集)
3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 436 Solved ...
- 关于ppt的字体(转载)
壹文钱:教程(13)——字体篇(上) 2015-11-25 @嘉文钱 幻方秋叶PPT 前三期嘉文给大家说了颜色,想复习的童鞋,链接在此: 壹文钱:教程(10)——色色,你好! 壹文钱:教程(11)—— ...
- 【java基础】java字符串之StringBuffer和StringBuilder
[一]简述区别 package com.sxf.test.string; public class StringBufferStringBuilderTest { public static void ...
- Linux内核gpiolib注册建立过程
1.相关的数据结构 struct s3c_gpio_chip { // 这个结构体是三星在移植gpiolib时封装的一个结构体 用来描述一组gpio端口信息 struct gpio_chip chip ...
- 设备树驱动API【原创】
#include <linux/init.h> #include <linux/module.h> #include <linux/of.h> #include & ...
- Oracle Database express 11g 第 2 版安装和配置
官方Oracle Database 快捷版 11g 第 2 版的下载地址: http://www.oracle.com/technetwork/cn/products/express-edition/ ...
- PHP代码实现 1
$PHP-SRC/run-test.php 因为如果在同一个进程中执行, 测试就会停止,后面的测试也将无法执行,php中有很多将脚本隔离的方法比如: system(),exec()等函数,这样可以使用 ...