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

Commit 26e89ad

Browse files
author
guangsheng.li01
committed
Solved 0118
1 parent 99154c7 commit 26e89ad

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/a0118_pascals_triangle.rs

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

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod a0035_search_insert_position;
1313
mod a0053_maximum_subarray;
1414
mod a0066_plus_one;
1515
mod a0088_merge_sorted_array;
16+
mod a0118_pascals_triangle;
1617
mod a0145_binary_tree_postorder_traversal;
1718
mod a0172_factorial_trailing_zeroes;
1819
mod a0400_nth_digit;

0 commit comments

Comments
 (0)