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]
.
题意:给定一个矩阵,按题目要求求出序列。。。
思路:直接循环模拟即可
C++ 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ans;
if(matrix.empty())
return ans;
int left = 0, right = matrix[0].size() - 1, top = 0, bottom = matrix.size() - 1;
while(left <= right && top <= bottom){
for(int i = left; i <= right; ++i)
ans.push_back(matrix[top][i]);
if(++top > bottom) break;
for(int i = top; i <= bottom; ++i)
ans.push_back(matrix[i][right]);
if(--right < left) break;
for(int i = right; i >= left; --i)
ans.push_back(matrix[bottom][i]);
if(--bottom < top) break;
for(int i = bottom; i >= top; --i)
ans.push_back(matrix[i][left]);
++left;
}
return ans;
}
};
Python 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
ans = []
if not matrix: return ans
top, bottom, left, right = 0, len(matrix) - 1, 0, len(matrix[0]) - 1
while left <= right and top <= bottom:
for i in range(left, right + 1):
ans.append(matrix[top][i])
top += 1
if top > bottom: break
for i in range(top, bottom + 1):
ans.append(matrix[i][right])
right -= 1
if right < left: break
for i in range(right, left - 1, -1):
ans.append(matrix[bottom][i])
bottom -= 1
if bottom < top: break
for i in range(bottom, top - 1, -1):
ans.append(matrix[i][left])
left += 1
return ans
leetcode Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to _n_2 in spiral order.
For example, Given n =
3
,You should return the following matrix:
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]
题目地址:leetcode Spiral Matrix II
题意:这题和上面一题的区别在于,这题是给n,求从1~n组从的序列
思路直接用上面的代码改改即可。
C++ 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ans(n, vector<int>(n));
int left = 0, right = n - 1, top = 0, bottom = n - 1;
int cur = 0;
while(left <= right && top <= bottom){
for(int i = left; i <= right; ++i)
ans[top][i] = ++cur;
if(++top > bottom) break;
for(int i = top; i <= bottom; ++i)
ans[i][right] = ++cur;
if(--right < left) break;
for(int i = right; i >= left; --i)
ans[bottom][i] = ++cur;
if(--bottom < top) break;
for(int i = bottom; i >= top; --i)
ans[i][left] = ++cur;
++left;
}
return ans;
}
};
Python 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35class Solution(object):
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
ans = [[0] * n for _ in range(n)]
top, bottom, left, right = 0, n - 1, 0, n - 1
cur = 1
while left <= right and top <= bottom:
for i in range(left, right + 1):
ans[top][i] = cur
cur += 1
top += 1
if top > bottom: break
for i in range(top, bottom + 1):
ans[i][right] = cur
cur += 1
right -= 1
if right < left: break
for i in range(right, left - 1, -1):
ans[bottom][i] = cur
cur += 1
bottom -= 1
if bottom < top: break
for i in range(bottom, top - 1, -1):
ans[i][left] = cur
cur += 1
left += 1
return ans