给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

示例 1:

输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 输出: [1,2,3,6,9,8,7,4,5]

示例 2:

输入: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] 输出: [1,2,3,4,8,12,11,10,9,5,6,7]

感觉很乱,有空的时候重新改一下

class Solution {
public:
vector<int> spiralOrder(vector<vector<int> >& matrix)
{
vector<int> res;
int r = matrix.size();
if(r == 0)
return res;
int c = matrix[0].size();
int all = r * c;
int up = 0;
int down = r - 1;
int right = c - 1;
int left = 0;
int cnt = 0;
int i = 0, j = 0;
int h = 0;
int v = 0;
for(int t = 0; cnt <= all; t++)
{
// horizontal
if(t % 2 == 0)
{
if(h % 2 == 0)
{
for(; j <= right; j++)
{
res.push_back(matrix[i][j]);
cnt++;
if(cnt == all)
break;
}
j--;
i++;
up++;
h++;
}
else
{
for(; j>= left; j--)
{
res.push_back(matrix[i][j]);
cnt++;
if(cnt == all)
break;
}
j++;
i--;
down--;
h++;
}
}
//vertical
else
{
if(v % 2 == 0)
{
for(; i <= down; i++)
{
res.push_back(matrix[i][j]);
cnt++;
if(cnt == all)
break;
}
i--;
j--;
right--;
v++;
}
else
{
for(; i >= up; i--)
{
res.push_back(matrix[i][j]);
cnt++;
if(cnt == all)
break;
}
i++;
j++;
left++;
v++;
}
}
if(cnt == all)
break;
}
return res;
}
};

其他方法:

class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> result;
if (matrix.size() == 0) {
return result;
}
int m = matrix.size(), n = matrix[0].size();
int rowBegin = 0, rowEnd = m - 1, colBegin = 0, colEnd = n - 1;
while (rowBegin <= rowEnd && colBegin <= colEnd) {
for (int i = colBegin; i <= colEnd; ++i ) {
result.push_back(matrix[rowBegin][i]);
}
rowBegin++;
if (rowBegin > rowEnd) {
break;
}
for (int i = rowBegin; i <= rowEnd; ++i) {
result.push_back(matrix[i][colEnd]);
}
colEnd--;
if (colBegin > colEnd) {
break;
}
for (int i = colEnd; i >= colBegin; --i) {
result.push_back(matrix[rowEnd][i]);
}
rowEnd--;
if (rowBegin > rowEnd) {
break;
}
for (int i = rowEnd; i>= rowBegin; --i) {
result.push_back(matrix[i][colBegin]);
}
colBegin++;
}
return result;
}
};

Leetcode54. Spiral Matrix螺旋矩阵的更多相关文章

  1. 第29题:LeetCode54:Spiral Matrix螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...

  2. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  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 ...

  4. PAT甲级——1105 Spiral Matrix (螺旋矩阵)

    此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...

  5. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  6. [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 ...

  7. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

  8. spiral matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. [算法][LeetCode]Spiral Matrix——螺旋矩阵

    题目要求 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spir ...

随机推荐

  1. Object.keys()应用

    <script type="text/javascript"> var person={ firstName:"David", lastName:& ...

  2. iOS开发CATransform3D.h属性详解和方法使用

    1.CATransform3D简介 layer有个属性transform,是CATransform3D类型.可以使其在三维界面作平移.缩放和旋转单独或组合动画! CATransform3D结构体: / ...

  3. linux操作mysql命令快速手记 — 让手指跟上思考的速度(二)

    这一篇是<mysql内建命令快速手记>的姐妹篇,废话不再赘述,直接上干货,跟老铁慢慢品 1.mysql -hlocalhost -uroot -proot,-h,-u,-p分别代表ip,u ...

  4. Linux常见问题解答--如何修复“tar:Exiting with failure status due to previous errors”

    问题: 当我用tar命令来创建一个压缩文件时,总在执行过程中失败,并且抛出一个错误说明"tar:由于前一个错误导致失败退出"("Exiting with failure ...

  5. Region服务器工作原理

  6. HBase 数据坐标

  7. pptp,l2tp获取登录用户信息用pppd参数即可

    这个问题困扰了我很久,终于在pppd的man文档里,发现了踪迹.在man中的SCRIPTS下有一系列的参数,其中PEERNAME就是登陆的用户名,并且在/etc/ppp/ip-up和/etc/ppp/ ...

  8. wsoj「G2016 SCOI2018 Round #12」建筑师

    传送门 小半个月前的测试,现在翻出来. 考试时我和sxyA了这题. 当时随便搞了个dp,dp[i][j]表示i个数能看到j个的情况数,考虑新加入一个比之前i-1个数都小的数,能看到它的情况是它加到第一 ...

  9. Trie性能分析之敏感词过滤golang

    package util import ( "strings" ) type Node struct { //rune表示一个utf8字符 char rune Data inter ...

  10. dom4j和document

    DOM的优点和缺点: 优点:DOM操作思维清晰,简单 缺点:在操作大量数据的时候性能,不能保证 DOM(Document Object Model) // 表示出需要被操作的XML文件的路径,注意是文 ...