跳到主要内容

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

分析

这题比上一题要简单。

代码 1

# Spiral Matrix II
# 时间复杂度O(n^2),空间复杂度O(n^2)
def generateMatrix(n):
matrix = [[0] * n for _ in range(n)]
begin = 0
end = n - 1
num = 1

while begin < end:
for j in range(begin, end): matrix[begin][j] = num; num += 1
for i in range(begin, end): matrix[i][end] = num; num += 1
for j in range(end, begin, -1): matrix[end][j] = num; num += 1
for i in range(end, begin, -1): matrix[i][begin] = num; num += 1
begin += 1
end -= 1

if begin == end:
matrix[begin][begin] = num

return matrix

代码 2

// LeetCode, Spiral Matrix II
// @author 龚陆安 (http://weibo.com/luangong)
// 时间复杂度O(n^2),空间复杂度O(n^2)
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector< vector<int> > matrix(n, vector<int>(n));
if (n == 0) return matrix;
int beginX = 0, endX = n - 1;
int beginY = 0, endY = n - 1;
int num = 1;
while (true) {
for (int j = beginX; j <= endX; ++j) matrix[beginY][j] = num++;
if (++beginY > endY) break;

for (int i = beginY; i <= endY; ++i) matrix[i][endX] = num++;
if (beginX > --endX) break;

for (int j = endX; j >= beginX; --j) matrix[endY][j] = num++;
if (beginY > --endY) break;

for (int i = endY; i >= beginY; --i) matrix[i][beginX] = num++;
if (++beginX > endX) break;
}
return matrix;
}
};

相关题目