-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha0867_transpose_matrix.rs
55 lines (44 loc) · 1.03 KB
/
a0867_transpose_matrix.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
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
/*
* [0867] transpose-matrix
*/
pub struct Solution {}
// solution impl starts here
impl Solution {
pub fn transpose(a: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
if a.len() == 0 {
return vec![];
}
let h = a.len() as usize;
let w = a[0].len() as usize;
let mut res = Vec::new();
for r in 0..w {
let mut row = Vec::new();
for c in 0..h {
row.push(a[c][r]);
}
res.push(row);
}
res
}
}
// solution impl ends here
// solution tests starts here
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_case0() {
assert_eq!(
Solution::transpose(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]]),
vec![[1, 4, 7], [2, 5, 8], [3, 6, 9]]
);
}
#[test]
fn test_case1() {
assert_eq!(
Solution::transpose(vec![vec![1, 2, 3], vec![4, 5, 6]]),
vec![[1, 4], [2, 5], [3, 6]]
);
}
}
// solution tests ends here