forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
30 lines (30 loc) · 806 Bytes
/
Solution.rs
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
impl Solution {
pub fn matrix_reshape(mat: Vec<Vec<i32>>, r: i32, c: i32) -> Vec<Vec<i32>> {
let r = r as usize;
let c = c as usize;
let m = mat.len();
let n = mat[0].len();
if m * n != r * c {
return mat;
}
let mut i = 0;
let mut j = 0;
(0..r)
.into_iter()
.map(|_| {
(0..c)
.into_iter()
.map(|_| {
let res = mat[i][j];
j += 1;
if j == n {
j = 0;
i += 1;
}
res
})
.collect()
})
.collect()
}
}