59. 螺旋矩陣 II:
給你一個(gè)正整數(shù) n
,生成一個(gè)包含 1 到 n2 所有元素,且元素按順時(shí)針順序螺旋排列的 n x n
正方形矩陣 matrix
。
樣例 1:
文章來源:http://www.zghlxwxcb.cn/news/detail-510767.html
輸入:
n = 3
輸出:
[[1,2,3],[8,9,4],[7,6,5]]
樣例 2:
輸入:
n = 1
輸出:
[[1]]
提示:
- 1 <= n <= 20
分析:
- 面對(duì)這道算法題目,二當(dāng)家的再次陷入了沉思。
- 可以每次循環(huán)移動(dòng)一步,判斷移到邊界就變換方向,巧用數(shù)組可以減少邏輯判斷的復(fù)雜性。
- 也可以每次循環(huán)都換完4次方向,也就是完成一次順時(shí)針,然后縮圈,就像剝洋蔥一樣,一層一層的從外向內(nèi),非常好理解。
- 和 54. 螺旋矩陣 非常類似,但是之前是讀,而現(xiàn)在相當(dāng)于是寫。
- 邊界的判斷和邊界的縮小依然是關(guān)鍵。
- 一個(gè)方向的最后位置,同樣可以作為下一個(gè)方向的開始值,要當(dāng)心不要重復(fù),方法是一樣的,只是細(xì)節(jié)上有差異。
- 相對(duì)來說,按層的方式,一次遍歷完成一個(gè)順時(shí)針比較直觀,邏輯更加清晰,不容易出錯(cuò),每次遍歷都完成相同的內(nèi)容。
- 每次循環(huán)完成一步的方式,要有個(gè)狀態(tài)切換的問題,把握不準(zhǔn)容易出錯(cuò)。
題解:
rust:
impl Solution {
pub fn generate_matrix(n: i32) -> Vec<Vec<i32>> {
let mut ans = vec![vec![0; n as usize]; n as usize];
let mut num = 0;
let (mut left, mut right, mut top, mut bottom) = (0, n as usize - 1, 0, n as usize - 1);
while left <= right && top <= bottom {
(left..right + 1).for_each(|column| {
num += 1;
ans[top][column] = num;
});
(top + 1..bottom + 1).for_each(|row| {
num += 1;
ans[row][right] = num;
});
if left < right && top < bottom {
(left..right).rev().for_each(|column| {
num += 1;
ans[bottom][column] = num;
});
(top + 1..bottom).rev().for_each(|row| {
num += 1;
ans[row][left] = num;
});
}
if right == 0 || bottom == 0 {
break;
}
left += 1;
right -= 1;
top += 1;
bottom -= 1;
}
return ans;
}
}
go:
func generateMatrix(n int) [][]int {
ans := make([][]int, n)
for i := range ans {
ans[i] = make([]int, n)
}
num := 0
left, right, top, bottom := 0, n-1, 0, n-1
for left <= right && top <= bottom {
for column := left; column <= right; column++ {
num++
ans[top][column] = num
}
for row := top + 1; row <= bottom; row++ {
num++
ans[row][right] = num
}
if left < right && top < bottom {
for column := right - 1; column >= left; column-- {
num++
ans[bottom][column] = num
}
for row := bottom - 1; row > top; row-- {
num++
ans[row][left] = num
}
}
left++
right--
top++
bottom--
}
return ans
}
c++:
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ans(n, vector<int>(n));
int num = 0;
int left = 0, right = n - 1, top = 0, bottom = n - 1;
while (left <= right && top <= bottom) {
for (int column = left; column <= right; ++column) {
ans[top][column] = ++num;
}
for (int row = top + 1; row <= bottom; ++row) {
ans[row][right] = ++num;
}
if (left < right && top < bottom) {
for (int column = right - 1; column >= left; --column) {
ans[bottom][column] = ++num;
}
for (int row = bottom - 1; row > top; --row) {
ans[row][left] = ++num;
}
}
++left;
--right;
++top;
--bottom;
}
return ans;
}
};
python:
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
ans = [[0] * n for _ in range(n)]
num = 0
left, right, top, bottom = 0, n - 1, 0, n - 1
while left <= right and top <= bottom:
for column in range(left, right + 1):
num += 1
ans[top][column] = num
for row in range(top + 1, bottom + 1):
num += 1
ans[row][right] = num
if left < right and top < bottom:
for column in range(right - 1, left - 1, -1):
num += 1
ans[bottom][column] = num
for row in range(bottom - 1, top, -1):
num += 1
ans[row][left] = num
left, right, top, bottom = left + 1, right - 1, top + 1, bottom - 1
return ans
java:
class Solution {
public int[][] generateMatrix(int n) {
final int[][] ans = new int[n][n];
int num = 0;
int left = 0, right = n - 1, top = 0, bottom = n - 1;
while (left <= right && top <= bottom) {
for (int column = left; column <= right; ++column) {
ans[top][column] = ++num;
}
for (int row = top + 1; row <= bottom; ++row) {
ans[row][right] = ++num;
}
if (left < right && top < bottom) {
for (int column = right - 1; column >= left; --column) {
ans[bottom][column] = ++num;
}
for (int row = bottom - 1; row > top; --row) {
ans[row][left] = ++num;
}
}
++left;
--right;
++top;
--bottom;
}
return ans;
}
}
非常感謝你閱讀本文~
歡迎【點(diǎn)贊】【收藏】【評(píng)論】~
放棄不難,但堅(jiān)持一定很酷~
希望我們大家都能每天進(jìn)步一點(diǎn)點(diǎn)~
本文由 二當(dāng)家的白帽子:https://le-yi.blog.csdn.net/ 博客原創(chuàng)~文章來源地址http://www.zghlxwxcb.cn/news/detail-510767.html
到了這里,關(guān)于算法leetcode|59. 螺旋矩陣 II(rust重拳出擊)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!