给定一个包含 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. js实现获取两个日期之间所有日期的方法

    function getDate(datestr){ var temp = datestr.split("-"); var date = new Date(temp[0],temp ...

  2. 我学习python没有记住的东西

    格式化 # 格式化 a=123 b='ww' print("%d,%s,%%"%(a,b)) # %d,%s,%f,%c,%f 格式化代码:print('{}{}'.format( ...

  3. [Ceoi2007]Royaltreasury

    #1945. [Ceoi2007]Royaltreasury Online Judge:Bzoj-1945 Label:树形Dp,高精度 题目描述 在很久很久以前的一个王国里,王国的财产开始变得越来越 ...

  4. JSOI 2016 扭动的字符串

    JSOI 2016 扭动的字符串 题面描述 给出两个长度为\(n\)的字符串\(A,B\) \(S(i,j,k)\)表示把\(A\)中的\([i,j]\)和\(B\)中的\([j,k]\)拼接起来的字 ...

  5. 阿里云 Aliplayer高级功能介绍(五):多语言

    基本介绍 Aliplayer默认支持中文和英文,并且依赖于浏览器的语言设置自动启用中文或英文资源, 除了支持这两种资源外,还提供自定义语言的形式,支持其他国际语言,另外Aliplayer还支持点播服务 ...

  6. Java虚拟机系列(三)---内存溢出情况及解决方法

    因为Java虚拟机内存有堆内存.方法区.虚拟机栈.本地方法栈和程序计数器五部分组成,其中程序计数器是唯一一块不会发生内存溢出异常的内存区,所以只有四类内存区可能发生内存溢出异常,其中虚拟机栈和本地方法 ...

  7. Cesium实现背景透明的方法

    前言 今天有人在Cesium实验室QQ群里问如何把地球背景做成透明的,当时我以为Cesium比较复杂的渲染机制可能即使context设置了alpha属性也未必能透明,所以和同学说可能得改Cesium代 ...

  8. Multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage; increase this mysqld variable and try again

    WARN: SQL Error: , SQLState: HY000 八月 , :: 下午 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logEx ...

  9. csdn vip文章:使用matlab模拟镜头失真

    原文地址 https://blog.csdn.net/lircsszz/article/details/80249017 最近在研究图像校正,现将镜头失真中常见的径向畸变(radial distort ...

  10. 【DM8168学习笔记3】CodSourcery GCC Tool Chain安装过程记录

    eagle@eagle-desktop:~$ cd/home/eagle/desktop eagle@eagle-desktop:~/desktop$ cd./vboxshared eagle@eag ...