Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit efedb60

Browse files
author
guangsheng.li01
committed
Solved 0119
1 parent 26e89ad commit efedb60

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/a0119_pascals_triangle_ii.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* [0119] pascals-triangle-ii
3+
*/
4+
5+
pub struct Solution {}
6+
7+
// solution impl starts here
8+
9+
impl Solution {
10+
pub fn get_row(row_index: i32) -> Vec<i32> {
11+
let row_index = row_index as usize;
12+
let mut v: Vec<i32> = (0..=row_index).map(|_| 1).collect();
13+
for r in 2..=row_index {
14+
let mut y = v[0];
15+
for c in 1..r {
16+
let x = v[c];
17+
v[c] += y;
18+
y = x;
19+
}
20+
}
21+
v
22+
}
23+
}
24+
25+
// solution impl ends here
26+
27+
// solution tests starts here
28+
29+
#[cfg(test)]
30+
mod tests {
31+
use super::*;
32+
33+
#[test]
34+
fn test_case0() {
35+
assert_eq!(Solution::get_row(0), vec![1]);
36+
}
37+
38+
#[test]
39+
fn test_case1() {
40+
assert_eq!(Solution::get_row(1), vec![1, 1]);
41+
}
42+
43+
#[test]
44+
fn test_case2() {
45+
assert_eq!(Solution::get_row(2), vec![1, 2, 1]);
46+
}
47+
48+
#[test]
49+
fn test_case3() {
50+
assert_eq!(Solution::get_row(3), vec![1, 3, 3, 1]);
51+
}
52+
}
53+
54+
// solution tests ends here

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod a0053_maximum_subarray;
1414
mod a0066_plus_one;
1515
mod a0088_merge_sorted_array;
1616
mod a0118_pascals_triangle;
17+
mod a0119_pascals_triangle_ii;
1718
mod a0145_binary_tree_postorder_traversal;
1819
mod a0172_factorial_trailing_zeroes;
1920
mod a0400_nth_digit;

0 commit comments

Comments
 (0)