leetcode54
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if (matrix.empty())
return vector<int>(); vector<int> ret; //输入矩阵行数
int m = matrix.size() - ; //输入矩阵的列数
int n = matrix[].size() - ; for (int x = , y = ; x <= m && y <= n; x++, y++)
{
//输出矩阵首行
for(int j=y ; j<=n ; ++j)
{
ret.push_back(matrix[x][j]);
}//while //输出矩阵最右列
for (int i = x + ; i <= m; ++i)
{
ret.push_back(matrix[i][n]);
}//while //输出矩阵最底行
for (int j = n - ; j >= y && x != m; --j)
{
ret.push_back(matrix[m][j]);
} //输出矩阵最左列
for (int i = m - ; i > x && y != n; --i)
{
ret.push_back(matrix[i][y]);
} m--;
n--;
}//for return ret;
}
};
补充一个使用dfs思想的python实现代码:
class Solution:
# matrix类型为二维列表,需要返回列表
def __init__(self):
self.result = []
self.visited = [] def goRight(self,matrix,i,j,row,column,direct):
if j + < column and self.visited[i][j+] == :
self.result.append(matrix[i][j+])
self.visited[i][j+] =
self.dfs(matrix,i,j+,row,column,direct) def goDown(self,matrix,i,j,row,column,direct):
if i + < row and self.visited[i+][j] == :
self.result.append(matrix[i+][j])
self.visited[i+][j] =
self.dfs(matrix,i+,j,row,column,direct) def goLeft(self,matrix,i,j,row,column,direct):
if j - >= and self.visited[i][j-] == :
self.result.append(matrix[i][j-])
self.visited[i][j-] =
self.dfs(matrix,i,j-,row,column,direct) def goUp(self,matrix,i,j,row,column,direct):
if i - >= and self.visited[i-][j] == :
self.result.append(matrix[i-][j])
self.visited[i-][j] =
self.dfs(matrix,i-,j,row,column,direct) def dfs(self,matrix,i,j,row,column,direct):
if direct == :
self.goRight(matrix,i,j,row,column,)
self.goDown(matrix,i,j,row,column,)
self.goLeft(matrix,i,j,row,column,)
self.goUp(matrix,i,j,row,column,)
if direct == :
self.goDown(matrix,i,j,row,column,)
self.goLeft(matrix,i,j,row,column,)
self.goUp(matrix,i,j,row,column,)
self.goRight(matrix,i,j,row,column,)
if direct == :
self.goLeft(matrix,i,j,row,column,)
self.goUp(matrix,i,j,row,column,)
self.goRight(matrix,i,j,row,column,)
self.goDown(matrix,i,j,row,column,)
if direct == :
self.goUp(matrix,i,j,row,column,)
self.goRight(matrix,i,j,row,column,)
self.goDown(matrix,i,j,row,column,)
self.goLeft(matrix,i,j,row,column,) def spiralOrder(self, matrix):
row = len(matrix)
if row == :
return []
column = len(matrix[])
self.visited = [[ for c in range(column)] for r in range(row)]
self.result.append(matrix[][])
self.visited[][] =
self.dfs(matrix,,,row,column,)
return self.result
# write code here
leetcode54的更多相关文章
- [Swift]LeetCode54. 螺旋矩阵 | Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 第29题:LeetCode54:Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...
- 算法练习--LeetCode--54. Spiral Matrix 100%
Spiral MatrixMedium Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- Leetcode54. Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...
- LeetCode54 Spiral Matrix
题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spira ...
- LeetCode54. 螺旋矩阵
题意是,输入一个二维数组,从数组左上角开始,沿着顺时针慢慢地"遍历"每一个元素且每一个元素只遍历一次, 在一个新的一维数组中记录遍历的顺序,最终的返回值就是这个数组. 思路:可以考 ...
- [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 59. 螺旋矩阵 II(Spiral Matrix II)
题目描述 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7 ...
- LeetCode通关:数组十七连,真是不简单
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/chefyuan/algorithm-base https://github.com/youngyangy ...
随机推荐
- MySQL--MODIFY COLUMN和ALTER COLUMN
=================================================== 在修改列时,可以使用ALTER TABLE MODIFY COLUMN 和ALTER TABL ...
- oracle 日期时间函数
ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 ...
- sql 变量赋值
mysql 的变量赋值如下: set @name='app' ; or set @name:='appfirst'; or with select select @appname:='you name ...
- 【转】每天一个linux命令(51):lsof命令
原文网址:http://www.cnblogs.com/peida/archive/2013/02/26/2932972.html lsof(list open files)是一个列出当前系统打开文件 ...
- JUC集合之 ConcurrentHashMap
ConcurrentHashMap介绍 ConcurrentHashMap是线程安全的哈希表. HashMap, Hashtable, ConcurrentHashMap之间的关联如下: HashMa ...
- ML(3.2): NavieBayes R_kalR
ML3.1 介绍e1071包实施朴素贝叶斯分类的函数,本例使用klaR包中的NaiveBayes函数,因为该函数较之前者增加了两个功能,一个是可以输入先验概率,另一个是在正态分布基础上增加了核平滑密度 ...
- 【appium】根据id定位元素
目前没有尝试成功,等成功后补充 id=Resource Id,可以通过UIAutomatorViewer获得.如果目标设备的API Level低于18则UIAutomatorViewer不能获得对应的 ...
- PHP中header的用法总结
来源 :http://blog.sina.com.cn/s/blog_7298f36f01011dxv.html header的用法 header()函数的作用是:发送一个原始 HTTP 标头[Htt ...
- spring eureka required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found.
spring在集成第三方过程很容易出现类名相同,且基本作用相同的类.这样给初学者带来一定的困惑. 导致用错类而出现以下问题. required a bean of type 'com.netflix. ...
- js阻止默认事件、拖拽等等
1.自定义右键菜单: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> &l ...