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 ...
随机推荐
- POJ2311 Cutting Game
题意 Language:Default Cutting Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6007 Acc ...
- 网卡驱动_WDS
参考 cs89x0.c 1.网卡驱动程序与网络驱动程序的区别网卡驱动程序:网络驱动程序中最底层的驱动,主要工作:把上面发下来的数据发送出去,收到数据后构造一个包抛给上层.有收发能力就可以了. 2.网卡 ...
- hasura graphql server 集成gatsby
hasura graphql server 社区基于gatsby-source-graphql 开发了gatsby-postgres-graphql 插件, 可以快速的开发丰富的网站 基本使用 安装h ...
- adnanh webhook 框架 hook rule
adnanh webhook 支持一系列的逻辑操作 AND 所有的条件都必须匹配 { "and": [ { "match": { "type" ...
- nyoj 一笔画问题
一笔画问题 描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来. 规定,所有的边都只能画一次,不能重复画. 输入 第一行只有一个正整 ...
- Spring Cloud Netflix项目进入维护模式
任何项目都有其生命周期,Spring Could Netflix也不例外,官宣已进入维护模式,如果在新项目开始考虑技术选型时要考虑到这点风险,并考虑绕道的可能性. 原创: itmuch IT牧场 这 ...
- npm 使用
npm 命令: 命令 作用 npm init 产生package.json文件 npm list <package> 查看安装的module版本 npm update <packag ...
- 使用fiddler进行genymotion安卓虚拟机手机抓包
1.首先先下载fiddler,这个直接百度就有啦. 2.打开fiddler ,可以看到这个界面还是挺帅的: 3.选择Tools - Fiddler Options -Https选项卡将配置设置为如下: ...
- JS 获取时间
function CurentTime() { var now = new Date(); var year = now.getFullYear(); //年 var month = now.getM ...
- 安装VS2012出问题后,反复重启电脑。
安装VS2012在装 VC++相关部分时出了问题,自动重启了. 重启后安装仍重启. 系统设置里取消了“自动重启”, 出现0x000000F4,网上搜索下,说是和硬盘有关,电源线.数据线.或电源不稳. ...