[Leetcode] 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].

题意:以螺旋的方式,顺时针访问数组。

思路:按照遍历的方向来不停的结果中压入值:左到右、上到下、右到左、下到上......一圈圈的向内访问。这里的难点有两处,一、圈与圈之间的过渡;二、若最后,只剩一行或者一列怎么办。针对第一个问题,我们可以定义四个变量,分别代表左右、上下,按照访问的顺序存入值。针对第二问题,若只剩一列,我们从上到下访问,并返回结果,只剩一行时,同理。还有一个问题是,访问时,变量 i 的取值范围。这可以按如下的方式取值,即,访问方向上,行或列的最后一个留个下一个循环去访问。

代码如下:

 1 class Solution {
 2 public:
 3     vector<int> spiralOrder(vector<vector<int> > &matrix)
 4     {
 5         vector<int> res;
 6         if(matrix.size()==0||matrix[0].size()==0)   return res;
 7
 8         int left=0,right=matrix[0].size()-1;
 9         int up=0,down=matrix.size()-1;
10
11         while(right>=left && down>=up)
12         {
13             if(right==left)     //单列
14             {
15                 for(int i=up;i<=down;++i)
16                 {
17                     res.push_back(matrix[i][right]);
18                 }
19                 return res;
20             }
21             else if(up==down)   //单行
22             {
23                 for(int i=left;i<=right;++i)
24                 {
25                     res.push_back(matrix[up][i]);
26                 }
27                 return res;
28             }
29
30             for(int i=left;i<right;++i)     //左到右
31                 res.push_back(matrix[up][i]);
32
33             for(int i=up;i<down;++i)        //上到下
34                 res.push_back(matrix[i][right]);
35
36             for(int i=right;i>left;i--)     //右到左
37                 res.push_back(matrix[down][i]);
38
39             for(int i=down;i>up;i--)        //下到上
40                 res.push_back(matrix[i][left]);
41
42             up++;
43             left++;
44             down--;
45             right--;
46         }
47         return res;
48     }
49 };
时间: 2024-10-25 18:14:31

[Leetcode] 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 the matrix in spiral order. 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1

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

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]. 思路:螺旋数组,需

LeetCode:Spiral Matrix I II

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]. 打印螺旋矩阵 逐个

LeetCode: Spiral Matrix [058]

[题目] 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]. [题意] 螺旋输出MxN矩阵 [注意

LeetCode: Spiral Matrix II [058]

[题目] Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] [题意] 给定整数n, 将1,2,3...nxn个数按螺旋旋转的方式填入nxn的矩

LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] SOLUTION 1: 还是与上一题Spiral Matrix类似

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

leetcode Spiral Matrix II

题目:是Spiral Matrix相关的的.这题的意思是给定一个n,那么在n*n的矩阵里按照循环记录将1,2,3,..., n*n.如下如果给定3,那么: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]一开始我想是不是有数学公式直接下标对应的,那直接遍历输出就可以了.但是推了一会,没有什么好的头绪.于是就分情况讨论了,和Spiral Matrix一样的分情况,只是这里的分情况是记录在数组里面.利用上,下,左,右,分别记录每次循环可以到达的四个边界,我们每次就输

LeetCode: Spiral Matrix 解题报告

Spiral MatrixGiven 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]. Solution 1: 使