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 ...
随机推荐
- day30 python学习 struct模块和 subprocess 模块
import subprocess import struct aa=input('>>') obj=subprocess.Popen(aa,shell=True,#aa代表的是读取字符串 ...
- C语言发送邮件
c语言发送邮件Linux下使用c语言发送邮件 领导交代一个任务,需要将服务器上的df -hl的执行结果定时发给他. 尝试使用sendmail来发邮件,但是后来放弃了,并不是所有的服务器上都安装了sen ...
- ZedGraph控件的使用
http://blog.chinaunix.net/uid-20776117-id-1847015.html 在我们编写程序的时候,有时候是要做一些统计的,为了达到一目了然的效果,饼状图,曲线图,柱状 ...
- JS 检测浏览器中是否安装了特定的插件
1.检测非IE浏览器 可以使用plugins数组来达到这个目的,例: //检测插件(在IE中无效) function hasPlugin(name){ name = name.toLowerCase( ...
- 【appium】keyevent的keycode
方法1 AppiumDriver实现了在上述功能,代码如下(java版本) driver.sendKeyEvent(66); 方法2 HashMap<String, Integer> ke ...
- asp.net Repeater使用例子,包括分页
<style type="text/css"> .tab{border-collapse:collapse; margin:0 auto;} .tab th ...
- 微信小程序开发(request请求后台获取不到data)
1微信的request的post请求后台获取不到data(当初这个问题纠结了好久好久),原因是post传递的data是json格式而不是key,value的格式,所以获取不到相应的data就是post ...
- phonegap 2.9 IOS Xcode 搭建环境
一:下载phoneGap2.9和安装Xcode5(目前最新版) 选择2.9是因为3.0以上坑爹版本编译神马的要在有网络情况. 二: 下载phonegap后解压到你的指定文件夹中,解压后找到create ...
- windows下Nginx反向代理服务器安装与配置
感谢慕课网Geely老师的讲解,本人将Nginx进行如下的总结. Nginx是一款轻量级的Web服务器,也是一款反向代理服务器,其主要特点:高稳定, 高性能,资源占用少功能丰富,模块化结构 支持热部署 ...
- DOM操作之获取HTML、文本和值
在前面的知识中,我们有提到一个text()方法用来获取文本,其实,在jQuery中,获取HTML和文本的方法有很多,下面依次演示这些方法. 在开始操作前,我们先在html中添加如下代码,后期所有的操作 ...