前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode59螺旋矩阵II

leetcode59螺旋矩阵II

原创
作者头像
用户11062199
发布2024-06-19 14:02:57
650
发布2024-06-19 14:02:57

LC59螺旋矩阵II

给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。

示例:

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

解法一:

代码语言:javascript
复制
 class Solution {
 public:
     vector<vector<int>> generateMatrix(int n) {
         vector<vector<int>> res(n, vector<int>(n, 0)); // 使用vector定义一个二维数组
         int startx = 0, starty = 0; // 定义每循环一个圈的起始位置
         int loop = n / 2; // 每个圈循环几次,例如n为奇数3,那么loop = 1 只是循环一圈,矩阵中间的值需要单独处理
         int mid = n / 2; // 矩阵中间的位置,例如:n为3, 中间的位置就是(1,1),n为5,中间位置为(2, 2)
         int count = 1; // 用来给矩阵中每一个空格赋值
         int offset = 1; // 需要控制每一条边遍历的长度,每次循环右边界收缩一位
         int i,j;
         while (loop --) {
             i = startx;
             j = starty;
 ​
             // 下面开始的四个for就是模拟转了一圈
             // 模拟填充上行从左到右(左闭右开)
             for (j; j < n - offset; j++) {
                 res[i][j] = count++;
             }
             // 模拟填充右列从上到下(左闭右开)
             for (i; i < n - offset; i++) {
                 res[i][j] = count++;
             }
             // 模拟填充下行从右到左(左闭右开)
             for (; j > starty; j--) {
                 res[i][j] = count++;
             }
             // 模拟填充左列从下到上(左闭右开)
             for (; i > startx; i--) {
                 res[i][j] = count++;
             }
 ​
             // 第二圈开始的时候,起始位置要各自加1, 例如:第一圈起始位置是(0, 0),第二圈起始位置是(1, 1)
             startx++;
             starty++;
 ​
             // offset 控制每一圈里每一条边遍历的长度
             offset += 1;
         }
 ​
         // 如果n为奇数的话,需要单独给矩阵最中间的位置赋值
         if (n % 2) {
             res[mid][mid] = count;
         }
         return res;
     }
 };

解法二:

cpp解法

代码语言:javascript
复制
 class Solution {
 public:
         int left = 0, right = n-1, top = 0, bottom = n-1;
         int count = 1, target = n * n;
         vector<vector<int>> res(n, vector<int>(n, 0));
         //for循环中变量定义成i或j的细节:按照通常的思维,i代表行,j代表列
         //这样,就可以很容易区分出来变化的量应该放在[][]的第一个还是第二个
         //对于变量的边界怎么定义:
             //从左向右填充:填充的列肯定在[left,right]区间
             //从上向下填充:填充的行肯定在[top,bottom]区间
             //从右向左填充:填充的列肯定在[right,left]区间
             //从下向上填充:填充的行肯定在[bootom,top]区间
         //通过上面的总结会发现边界的起始和结束与方向是对应的
         while(count <= target){
             //从左到右填充,相当于缩小上边界
             for(int j = left; j <= right; j++) res[top][j] = count++;
             //缩小上边界
             top++;
             //从上向下填充,相当于缩小右边界
             for(int i = top; i <=bottom; i++) res[i][right] = count++;
             //缩小右边界
             right--;
             //从右向左填充,相当于缩小下边界
             for(int j = right; j >= left; j--) res[bottom][j] = count++;
             //缩小下边界
             bottom--;
             //从下向上填充,相当于缩小左边界
             for(int i = bottom; i >= top; i--) res[i][left] = count++;
             //缩小左边界
             left++;
         }
         return res;
 }

php解法

代码语言:javascript
复制
 class Solution{
     public function generateMatrix($n): array{
         $left = 0;$right = $n-1; $top = 0; $bottom = $n-1;
         $count = 1;$target = $n * $n;
         $res=array_fill(0,$n,array_fill(0,$n,0));
         while ($count <= $target) {
             // 从左到右填充,相当于缩小上边界
             for ($j = $left; $j <= $right; $j++) {
                 $res[$top][$j] = $count++;
             }
             // 缩小上边界
             $top++;
 ​
             // 从上向下填充,相当于缩小右边界
             for ($i = $top; $i <= $bottom; $i++) {
                 $res[$i][$right] = $count++;
             }
             // 缩小右边界
             $right--;
 ​
             // 从右向左填充,相当于缩小下边界
             for ($j = $right; $j >= $left; $j--) {
                 $res[$bottom][$j] = $count++;
             }
             // 缩小下边界
             $bottom--;
 ​
             // 从下向上填充,相当于缩小左边界
             for ($i = $bottom; $i >= $top; $i--) {
                 $res[$i][$left] = $count++;
             }
             // 缩小左边界
             $left++;
         }
         return $res;
     }
 }
代码语言:javascript
复制
 //cpp解法
 public class Solution{
     public int[][] generateMatrix(int n){
         int left = 0, right = n-1, top = 0, bottom = n-1;
         int count = 1, target = n * n;
         int[][] res=new int[n][n];
         while(count <= target){
             //从左到右填充,相当于缩小上边界
             for(int j = left; j <= right; j++) res[top][j] = count++;
             //缩小上边界
             top++;
             //从上向下填充,相当于缩小右边界
             for(int i = top; i <=bottom; i++) res[i][right] = count++;
             //缩小右边界
             right--;
             //从右向左填充,相当于缩小下边界
             for(int j = right; j >= left; j--) res[bottom][j] = count++;
             //缩小下边界
             bottom--;
             //从下向上填充,相当于缩小左边界
             for(int i = bottom; i >= top; i--) res[i][left] = count++;
             //缩小左边界
             left++;
         }
         return res;
     }
 }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • LC59螺旋矩阵II
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档