当先锋百科网

首页 1 2 3 4 5 6 7


59. 螺旋矩阵 II:

给你一个正整数 n ,生成一个包含 1n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix

样例 1:

输入:
	
	n = 3
	
输出:
	
	[[1,2,3],[8,9,4],[7,6,5]]

样例 2:

输入:
	
	n = 1
	
输出:
	
	[[1]]

提示:

  • 1 <= n <= 20

分析:


题解:

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;
    }
}

非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由
二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~