近来刷LeetCode上面的题。由于是纯菜鸟,很多特简单的题都不太会,而且很多题自己设计的算法,复杂度太高,希望能借助一下这个平台,能够学到大神们更好的算法(C#实现)。
这一题较简单。直接遍历一遍目标矩阵即可。
直接上代码:
public class Solution {
public int[,] MatrixReshape(int[,] nums, int r, int c) {
int[,] num = new int[r, c];
if (nums.GetLength(0) * nums.GetLength(1) == r * c)
{
int curren = 0;
foreach (var item in nums)
{
int i = curren / c;
int j = curren - i * c;
num[i, j] = item;
curren++;
}
return num;
}
else
{
return nums;
}
}
}
本文介绍了一个简单的LeetCode题目——矩阵重塑。通过遍历原始矩阵,根据新的行数和列数重新构建矩阵。若新矩阵尺寸不符合原始矩阵元素数量,则返回原矩阵。
&spm=1001.2101.3001.5002&articleId=80112470&d=1&t=3&u=e41ca4146ae54735866267d18a147ec3)
1091

被折叠的 条评论
为什么被折叠?



