-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path566.重塑矩阵.cs
78 lines (75 loc) · 1.77 KB
/
566.重塑矩阵.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* @lc app=leetcode.cn id=566 lang=csharp
*
* [566] 重塑矩阵
*
* https://leetcode-cn.com/problems/reshape-the-matrix/description/
*
* algorithms
* Easy (67.82%)
* Likes: 269
* Dislikes: 0
* Total Accepted: 81K
* Total Submissions: 119.5K
* Testcase Example: '[[1,2],[3,4]]\n1\n4'
*
* 在 MATLAB 中,有一个非常有用的函数 reshape ,它可以将一个 m x n 矩阵重塑为另一个大小不同(r x
* c)的新矩阵,但保留其原始数据。
*
* 给你一个由二维数组 mat 表示的 m x n 矩阵,以及两个正整数 r 和 c ,分别表示想要的重构的矩阵的行数和列数。
*
* 重构后的矩阵需要将原始矩阵的所有元素以相同的 行遍历顺序 填充。
*
* 如果具有给定参数的 reshape 操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。
*
*
*
* 示例 1:
*
*
* 输入:mat = [[1,2],[3,4]], r = 1, c = 4
* 输出:[[1,2,3,4]]
*
*
* 示例 2:
*
*
* 输入:mat = [[1,2],[3,4]], r = 2, c = 4
* 输出:[[1,2],[3,4]]
*
*
*
*
* 提示:
*
*
* m == mat.length
* n == mat[i].length
* 1 <= m, n <= 100
* -1000 <= mat[i][j] <= 1000
* 1 <= r, c <= 300
*
*
*/
// @lc code=start
public class Solution {
public int[][] MatrixReshape(int[][] mat, int r, int c) {
int m = mat.Length;
int n = mat[0].Length;
if (m * n != r * c) {
return mat;
}
int index = 0;
var result = new int[r][];
for (int i = 0; i < r; i++)
{
result[i] = new int[c];
for (int j = 0; j < c; j++) {
result[i][j] = mat[index / n][index % n];
index += 1;
}
}
return result;
}
}
// @lc code=end