-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray566.java
More file actions
79 lines (77 loc) · 2.14 KB
/
Array566.java
File metadata and controls
79 lines (77 loc) · 2.14 KB
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
78
79
package array;
/**
* @ProjectName: leetcode
* @Package: array
* @ClassName: Array566
* @Author: markey
* @Description:
* 在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据。
*
* 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重构的矩阵的行数和列数。
*
* 重构后的矩阵需要将原始矩阵的所有元素以相同的行遍历顺序填充。
*
* 如果具有给定参数的reshape操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。
*
* 示例 1:
*
* 输入:
* nums =
* [[1,2],
* [3,4]]
* r = 1, c = 4
* 输出:
* [[1,2,3,4]]
* 解释:
* 行遍历nums的结果是 [1,2,3,4]。新的矩阵是 1 * 4 矩阵, 用之前的元素值一行一行填充新矩阵。
* 示例 2:
*
* 输入:
* nums =
* [[1,2],
* [3,4]]
* r = 2, c = 4
* 输出:
* [[1,2],
* [3,4]]
* 解释:
* 没有办法将 2 * 2 矩阵转化为 2 * 4 矩阵。 所以输出原矩阵。
* 注意:
*
* 给定矩阵的宽和高范围在 [1, 100]。
* 给定的 r 和 c 都是正数。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/reshape-the-matrix
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2019/12/1 21:15
* @Version: 1.0
*/
public class Array566 {
/**
* 执行用时 :1 ms, 在所有 java 提交中击败了100.00%的用户
* 内存消耗 :39.2 MB, 在所有 java 提交中击败了97.17%的用户
* @param nums
* @param r
* @param c
* @return
*/
public int[][] matrixReshape(int[][] nums, int r, int c) {
if ((r * c) != nums.length * nums[0].length) {
return nums;
}
int[][] res = new int[r][c];
int i = 0, j = 0;
for (int[] col: nums){
for (int num: col) {
if (j >= c) {
j %= c;
i ++;
}
res[i][j] = num;
j ++;
}
}
return res;
}
}