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 ...
随机推荐
- filebeat 简介安装
Filebeat is a lightweight shipper for forwarding and centralizing log data. Installed as an agent on ...
- 001-Bitcoin比特币与BlockChain区块链技术
一.比特币历史 2008 年 10 月 31 日,一个网名叫中本聪(英文翻译过来滴)的家伙发布比特币唯一的白皮书:<Bitcoin:A Peer-to-PeerElectronic Cash S ...
- PyMySQL介绍
pymysql介绍 PyMySQL介绍 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb Django中也可以使用PyMySQ ...
- git---控制面板提交
比如我修改了一个项目的代码.需要提交代码. 1.打开项目所在目录,右键>Git Bash Here 2.打开交互模式.git会列出所有untracked的文件,然后你可以用各种形式加入.git ...
- Flume1.7.0的安装与使用
Flume下载后,解压,新增一个配置文件,写入配置即可 我将配置文件写在 conf 下,取名为 flume-conf-spooldir.properties Flume 运行命令: bin/flume ...
- ASP.NET MVC CheckBoxFor的int to bool
当我们使用CheckBoxFor类型需要使用bool ,可以将 int转换成bool <div class="form-group"> <label class= ...
- NOIP Mayan游戏
描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定的步数内消 ...
- 不错的ptyhon学习网站【学习笔记】
菜鸟教程: http://www.runoob.com/python/python-tutorial.html
- Spring_通过工厂方法配置 Bean
beans-factory.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns=&q ...
- 低版本C++ string的万能转换,从long string 之间的转换来看看
string 转 long 那必须是万年atoi(),不过得配合c_str()使用! [plain] view plain copy #include <string> #include ...