近来刷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;
}
}
}