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 ...
随机推荐
- struts 文件下载 annotation 注解版
[本文简介] 本文将简单介绍使用 struts2 ,通过零配置和 annotation 实现文件下载功能. [文件夹结构] [web.xml有关struts的配置] <filter> &l ...
- Android_Kotlin 代码学习
https://github.com/ldm520/Android_Kotlin_Demo
- eslasticsearch操作集锦
索引-index:一个索引就是一个拥有几分相似特征的文档的集合.比如说,你可以有一个客户数据的索引,另一个产品目录的索引,还有一个订单数据的索引.一个索引由一个名字来标识(必须全部是小写字母的),并且 ...
- sqlite时间段查询
同样的SQL语句,查不出数据来 select * from table1 where t1>='2017-6-1' and t1<='2017-6-5' 改成 select * from ...
- VGA显示
VGA控制器的编写主要是了解VGA的显示标准和时序,如1024X768@60Hz,确定时钟频率(65MHz=1344X806X60),列像素时间等于时钟周期,扫描从左到右.从上到下(类似于电视扫描PA ...
- 中文Appium API 文档
该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...
- python全栈开发从入门到放弃之字典的应用
1.存值 info_dic={'name':'egon','age':18,'sex':'male'} info_dic['job']='IT' #根据key来存值 print(info_dic) 输 ...
- .net:easyui-datagrid清空表中原有数据
$("#StudentTable").datagrid("loadData", { total: 0, rows: [] });
- 三道半平面交测模板题 Poj1474 Poj 3335 Poj 3130
求半平面交的算法是zzy大神的排序增量法. ///Poj 1474 #include <cmath> #include <algorithm> #include <cst ...
- 杭电1025Constructing Roads In JGShining's Kingdom
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1025 题目: Problem Description JGShining's kingdom consis ...