【leetcode】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].

如果下一步会遇到访问过的节点或者越界的节点,那么就转向。

转向按照右,下,左,上

 1 class Solution {
 2 public:
 3     vector<int> spiralOrder(vector<vector<int> > &matrix) {
 4         int m=matrix.size();
 5         if(m==0) return vector<int>();
 6
 7         int n=matrix[0].size();
 8
 9         vector<int> result(m*n);
10
11         vector<vector<bool> > visited(m,vector<bool>(n,false));
12
13         vector<pair<int,int>> dir(4);
14         dir[0]=pair<int,int>(0,1);
15         dir[1]=pair<int,int>(1,0);
16         dir[2]=pair<int,int>(0,-1);
17         dir[3]=pair<int,int>(-1,0);
18
19         int i,j,k,count;
20         i=j=k=count=0;
21
22         while(1)
23         {
24             if(count==m*n) break;
25
26             result[count]=matrix[i][j];
27             visited[i][j]=true;
28
29             if(i+dir[k].first>m-1||j+dir[k].second>n-1||i+dir[k].first<0||j+dir[k].second<0||visited[i+dir[k].first][j+dir[k].second])
30             {
31                 k=(++k)%4;
32             }
33
34             i+=dir[k].first;
35             j+=dir[k].second;
36
37             count++;
38         }
39
40         return result;
41     }
42 };

另外一种方法:

 1 class Solution {
 2 public:
 3     vector<int> spiralOrder(vector<vector<int> > &matrix) {
 4         int m=matrix.size();
 5         if(m==0) return vector<int>();
 6
 7         int n=matrix[0].size();
 8
 9         vector<int> result;
10
11
12         int x1=0;
13         int y1=0;
14         int x2=m-1;
15         int y2=n-1;
16
17
18         while(1)
19         {
20
21             for(int j=y1;j<=y2;j++) result.push_back(matrix[x1][j]);
22             x1++;
23
24
25             for(int i=x1;i<=x2;i++) result.push_back(matrix[i][y2]);
26             y2--;
27
28
29             if(x2+1!=x1)
30                 for(int j=y2;j>=y1;j--) result.push_back(matrix[x2][j]);
31             x2--;
32
33             if(y1!=y2+1)
34                 for(int i=x2;i>=x1;i--) result.push_back(matrix[i][y1]);
35
36             y1++;
37
38             if(result.size()==m*n) break;
39         }
40
41         return result;
42     }
43
44 };
时间: 2024-10-04 03:36:28

【leetcode】Spiral Matrix的相关文章

【LeetCode】Spiral Matrix II

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 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

【leetcode】Spiral Matrix(middle)

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]. 思路:就按照螺旋矩阵的规律 用n记录旋转圈数 每

【Leetcode】Spiral Matrix 一 和 二 in JAVA

首先是1: 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]. 我的思路是:设定一个moveSte

【Leetcode】Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Could you devise a constant space solution? 思路:因为需要遍历整个矩阵,时间复杂度肯定需要O(m * n),对于空间复杂度而言,第一种是可以使用O(m * n),对每个位置的0的情况进行记录,第二种是使用O(m + n),对每行每列是否存在0进行记录,第三种是O(1)

【LeetCode】Set Matrix Zeroes 解题报告

今天看到CSDN博客的勋章换了图表,同时也增加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward solution using O(mn) spac

【leecode】 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]. 题意很简单,就是顺

【数组】Spiral Matrix II

题目: 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 ] ] 思路: 本质上和上一题是一样的,这里我们要用数字螺旋的去填充矩阵.同理,我们也是逐个环

【数组】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]. 思路: 逐个环的打印, 对于m *n的矩

【leetcode】Set Matrix Zeros

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(