Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 32ca543

Browse files
author
guangsheng.li01
committed
Solved 0867
1 parent d40931e commit 32ca543

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

‎src/a0867_transpose_matrix.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* [0867] transpose-matrix
3+
*/
4+
5+
pub struct Solution {}
6+
7+
// solution impl starts here
8+
9+
impl Solution {
10+
pub fn transpose(a: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
11+
if a.len() == 0 {
12+
return vec![];
13+
}
14+
15+
let h = a.len() as usize;
16+
let w = a[0].len() as usize;
17+
let mut res = Vec::new();
18+
19+
for r in 0..w {
20+
let mut row = Vec::new();
21+
for c in 0..h {
22+
row.push(a[c][r]);
23+
}
24+
res.push(row);
25+
}
26+
res
27+
}
28+
}
29+
30+
// solution impl ends here
31+
32+
// solution tests starts here
33+
34+
#[cfg(test)]
35+
mod tests {
36+
use super::*;
37+
38+
#[test]
39+
fn test_case0() {
40+
assert_eq!(
41+
Solution::transpose(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]]),
42+
vec![[1, 4, 7], [2, 5, 8], [3, 6, 9]]
43+
);
44+
}
45+
46+
#[test]
47+
fn test_case1() {
48+
assert_eq!(
49+
Solution::transpose(vec![vec![1, 2, 3], vec![4, 5, 6]]),
50+
vec![[1, 4], [2, 5], [3, 6]]
51+
);
52+
}
53+
}
54+
55+
// solution tests ends here

‎src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod a0404_sum_of_left_leaves;
2323
mod a0405_convert_a_number_to_hexadecimal;
2424
mod a0617_merge_two_binary_trees;
2525
mod a0643_maximum_average_subarray_i;
26+
mod a0867_transpose_matrix;
2627
mod a0929_unique_email_addresses;
2728
mod a0931_minimum_falling_path_sum;
2829
mod a0937_reorder_data_in_log_files;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /