LeetCode OJ:Spiral Matrix(螺旋矩阵)
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]
.
典型的dfs问题,与前面一个问题比较像,代码如下。单独维护一个数组记录是否已经走过某个格子,注意边界条件的判断就可以了,代码如下:
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
drt = vector<vector<int>>{{,},{,},{-,},{,-}}; //四个方向
mark = vector<vector<bool>>(,vector<bool>(,false));
ret.clear();
if(!matrix.size() || !matrix[].size())
return ret;
dfs(matrix, , , -);//这里取-1是为了能从(0,0)开始。
return ret;
} void dfs(vector<vector<int>> & matrix, int direct, int x, int y)
{
for(int i = ; i < ; ++i){
int j = (direct + i) % ;
int tx = x + drt[j][];
int ty = y + drt[j][];
if(tx >= && tx < matrix.size() &&
ty >= && ty < matrix[].size() &&
mark[tx][ty] == false){
mark[tx][ty] = true;
ret.push_back(matrix[tx][ty]);
dfs(matrix, j, tx, ty);
}
}
} private:
vector<vector<int>> drt;
vector<vector<bool>> mark;
vector<int> ret;
};
LeetCode OJ:Spiral Matrix(螺旋矩阵)的更多相关文章
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- [leetcode]54. Spiral Matrix螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- PAT甲级——1105 Spiral Matrix (螺旋矩阵)
此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...
- [算法][LeetCode]Spiral Matrix——螺旋矩阵
题目要求 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spir ...
- 第29题:LeetCode54:Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...
- Leetcode54. Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...
- spiral matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
随机推荐
- 生成vuejs项目
生成项目 npm i -g vue-cli > mkdir my-project && cd my-project > vue init webpack npm i ...
- 玩玩nmap
---恢复内容开始--- [root@miyan ~]# nmap -v Starting Nmap 7.12 ( https://nmap.org ) at 2016-04-04 15:34 CST ...
- BLOG总结
1.登录:http://www.cnblogs.com/shaojiafeng/p/7868195.html 2.注册 - urls -前端页面中写 username ,password,passwo ...
- begoo——路由设置
路由本质是URL与要为该URL调用的视图函数之间的映射表,其实就是你定义的使用那个URL调用那段代码的关系对应表. 首先看一下最简单的路由: package routers import ( &quo ...
- Changing an Elements innerHTML in TWebBrowser
I'm unable to change the innerHTML of a javascript element, but i can change the id so i'm not sure ...
- 112. Path Sum (判断路径和是否等于某值)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up a ...
- Tomcat 线程池配置
线程池 Executor代表了一个线程池,可以在Tomcat组件之间共享.使用线程池的好处在于减少了创建销毁线程的相关消耗,而且可以提高线程的使用效率.要想使用线程池,首先需要在 Service标签中 ...
- HGVS,非HGVS形式的突变描述解释
NG_012232.1(NM_004006.1):c.93+1G>T: 在该转录本NM_004006.1外显子的第93个碱基的下1个碱基(属于内含子)G变为T. NG_012232.1(NM_0 ...
- 函数:生成n个互不相同的随机数,最大值为upper
参考:http://blog.csdn.net/zhangkaihang/article/details/6836506 函数getRandArray()功能说明: 入参:int upper-生成的随 ...
- SpringBoot 加载配置文件
1.application.properties或application.yaml是SpringBoot默认的配置文件. 可以通过@Value注解 配合 ${......}来读取配置在属性文件中的内容 ...