0


算法leetcode|59. 螺旋矩阵 II(rust重拳出击)


文章目录


59. 螺旋矩阵 II:

给你一个正整数

  1. n

,生成一个包含 1n2 所有元素,且元素按顺时针顺序螺旋排列的

  1. n x n

正方形矩阵

  1. matrix

样例 1:

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

样例 2:

  1. 输入:
  2. n = 1
  3. 输出:
  4. [[1]]

提示:

  • 1 <= n <= 20

分析:

  • 面对这道算法题目,二当家的陷入了沉思。
  • 可以每次循环移动一步,判断移到边界就变换方向。
  • 也可以每次循环都换完4次方向,也就是完成一次顺时针,然后缩圈。
  • 和 54. 螺旋矩阵 非常类似。

题解:

rust:

  1. implSolution{pubfngenerate_matrix(n:i32)->Vec<Vec<i32>>{letmut ans =vec![vec![0; n asusize]; n asusize];letmut num =0;let(mut left,mut right,mut top,mut bottom)=(0, n asusize-1,0, n asusize-1);while left <= right && top <= bottom {(left..right +1).for_each(|column|{
  2. num +=1;
  3. ans[top][column]= num;});(top +1..bottom +1).for_each(|row|{
  4. num +=1;
  5. ans[row][right]= num;});if left < right && top < bottom {(left..right).rev().for_each(|column|{
  6. num +=1;
  7. ans[bottom][column]= num;});(top +1..bottom).rev().for_each(|row|{
  8. num +=1;
  9. ans[row][left]= num;});}if right ==0|| bottom ==0{break;}
  10. left +=1;
  11. right -=1;
  12. top +=1;
  13. bottom -=1;}return ans;}}

go:

  1. funcgenerateMatrix(n int)[][]int{
  2. ans :=make([][]int, n)for i :=range ans {
  3. ans[i]=make([]int, n)}
  4. num :=0
  5. left, right, top, bottom :=0, n-1,0, n-1for left <= right && top <= bottom {for column := left; column <= right; column++{
  6. num++
  7. ans[top][column]= num
  8. }for row := top +1; row <= bottom; row++{
  9. num++
  10. ans[row][right]= num
  11. }if left < right && top < bottom {for column := right -1; column >= left; column--{
  12. num++
  13. ans[bottom][column]= num
  14. }for row := bottom -1; row > top; row--{
  15. num++
  16. ans[row][left]= num
  17. }}
  18. left++
  19. right--
  20. top++
  21. bottom--}return ans
  22. }

c++:

  1. classSolution{public:
  2. vector<vector<int>>generateMatrix(int n){
  3. 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){
  4. ans[top][column]=++num;}for(int row = top +1; row <= bottom;++row){
  5. ans[row][right]=++num;}if(left < right && top < bottom){for(int column = right -1; column >= left;--column){
  6. ans[bottom][column]=++num;}for(int row = bottom -1; row > top;--row){
  7. ans[row][left]=++num;}}++left;--right;++top;--bottom;}return ans;}};

python:

  1. classSolution:defgenerateMatrix(self, n:int)-> List[List[int]]:
  2. ans =[[0]* n for _ inrange(n)]
  3. num =0
  4. left, right, top, bottom =0, n -1,0, n -1while left <= right and top <= bottom:for column inrange(left, right +1):
  5. num +=1
  6. ans[top][column]= num
  7. for row inrange(top +1, bottom +1):
  8. num +=1
  9. ans[row][right]= num
  10. if left < right and top < bottom:for column inrange(right -1, left -1,-1):
  11. num +=1
  12. ans[bottom][column]= num
  13. for row inrange(bottom -1, top,-1):
  14. num +=1
  15. ans[row][left]= num
  16. left, right, top, bottom = left +1, right -1, top +1, bottom -1return ans

java:

  1. classSolution{publicint[][]generateMatrix(int n){finalint[][] ans =newint[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){
  2. ans[top][column]=++num;}for(int row = top +1; row <= bottom;++row){
  3. ans[row][right]=++num;}if(left < right && top < bottom){for(int column = right -1; column >= left;--column){
  4. ans[bottom][column]=++num;}for(int row = bottom -1; row > top;--row){
  5. ans[row][left]=++num;}}++left;--right;++top;--bottom;}return ans;}}

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


标签: rust 算法 golang

本文转载自: https://blog.csdn.net/leyi520/article/details/131374465
版权归原作者 二当家的白帽子 所有, 如有侵权,请联系我们删除。

“算法leetcode|59. 螺旋矩阵 II(rust重拳出击)”的评论:

还没有评论