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 ...
随机推荐
- 第30课 C语言中的字符串
任意的软件开发过程都会涉及到字符串,字符串的概念: ca是字符数组,其他几个都是字符串,因为最后都有'\0'. 示例程序如下: #include <stdio.h> int main() ...
- MySQL 主从复制与读写分离
1.MySQL主从复制入门 首先,我们看一个图: 影响MySQL-A数据库的操作,在数据库执行后,都会写入本地的日志系统A中. 假设,实时的将变化了的日志系统中的数据库事件操作,在MYSQL-A的33 ...
- 6-17 Shortest Path [2](25 分)
Write a program to find the weighted shortest distances from any vertex to a given source vertex in ...
- [团队项目]Scrum 项目1.0 (演说视频)
1.确定选题. 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 截止日期:2016.5.6日晚10点 2.SCRUM 流 ...
- Java多线程编程核心技术,第六章
1,饿汉模式/单例模式,一开始就新建一个静态变量,后面用getInstance()都是同一个变量 2,懒汉模式/单例模式,在getInstance()才会new一个对象,在第一个有了后不会继续创建 3 ...
- [您有新的未分配科技点]可,可,可持久化!?------0-1Trie和可持久化Trie普及版讲解
这一次,我们来了解普通Trie树的变种:0-1Trie以及在其基础上产生的可持久化Trie(其实,普通的Trie也可以可持久化,只是不太常见) 先简单介绍一下0-1Trie:一个0-1Trie节点只有 ...
- SQL中減少日志文件大小
SQL中減少日志文件大小 编写人:CC阿爸 2014-6-14 在日常SQL数据库的操作中,常常会出现SQL日志文件超大,大小都超过正常MDF数据库文件,作为一般用户来讲,LDF太大,只会影响服务 ...
- 移动端开发之px,em和rem详解
px:表示的是绝对的像素值,1px就是1像素大小 em:关于em,网上有资料说是关于父元素的,但是其实个人感觉这种说法是不对的,其实em的大小是根据自身的font-size确定的,而只是正常的情况下子 ...
- 关于 android百度地图 调用 地理位置 经纬度坐标,只调用一次的解决方法,通知栏不总是 搜索 GPS 。。。
上代码吧... //读取当前坐标 final LocationClient mLocationClient = new LocationClient(mActivity); mLocationClie ...
- 添加APP右上角数字提醒标识
mui.plusReady(function() { plus.nativeUI.closeWaiting(); mui.currentWebview.show(); //1.设置app右上角数字提醒 ...