-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path0566-ReshapeTheMatrix.cs
38 lines (35 loc) · 1.11 KB
/
0566-ReshapeTheMatrix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//-----------------------------------------------------------------------------
// Runtime: 260ms
// Memory Usage: 33.7 MB
// Link: https://leetcode.com/submissions/detail/336961634/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0566_ReshapeTheMatrix
{
public int[][] MatrixReshape(int[][] nums, int r, int c)
{
var rows = nums.Length;
var cols = nums[0].Length;
if (rows * cols != r * c) return nums;
int[][] result = new int[r][];
int row = 0, col = 0;
result[row] = new int[c];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result[row][col++] = nums[i][j];
if (col == c)
{
row++;
if (row < r)
result[row] = new int[c];
col = 0;
}
}
}
return result;
}
}
}