diff --git a/src/main.rs b/src/main.rs index a83d6df..53a1620 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,10 @@ use crate::fetch_problem::{Fetcher, ProblemManager}; +use anyhow::anyhow; +use serde::de::Error; use std::fs; use std::fs::exists; use std::io::Write; use std::path::Path; -use anyhow::anyhow; -use serde::de::Error; mod fetch_problem; @@ -46,7 +46,7 @@ async fn main() { fn write_file(file_name: &String, file_content: &String) -> anyhow::Result<()> { let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name)); - + if exists(&file_path)? { println!("{} has pulled.", file_name); return Err(anyhow!("{} has pulled", file_name)); diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 4adb6f8..82c34d4 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -456,3 +456,5 @@ mod p2944_minimum_number_of_coins_for_fruits; mod p2412_minimum_money_required_before_transactions; mod p40_combination_sum_ii; + +mod p119_pascals_triangle_ii; diff --git a/src/problem/p119_pascals_triangle_ii.rs b/src/problem/p119_pascals_triangle_ii.rs new file mode 100644 index 0000000..9798da8 --- /dev/null +++ b/src/problem/p119_pascals_triangle_ii.rs @@ -0,0 +1,43 @@ +/** + * [119] Pascal's Triangle II + */ +pub struct Solution {} + +// submission codes start here +//0 1 +//1 1 1 +//2 1 2 1 +//3 1 3 3 1 +//4 1 4 6 4 1 +//5 1 5 10 10 5 1 +//6 1 6 15 20 15 6 1 + +impl Solution { + pub fn get_row(row_index: i32) -> Vec { + let row_index = row_index as usize; + let mut matrix = vec![vec![0; row_index + 2]; row_index + 1]; + matrix[0][1] = 1; + + for i in 1..=row_index { + for j in 1..=i + 1 { + matrix[i][j] = matrix[i - 1][j - 1] + matrix[i - 1][j]; + } + } + + matrix[row_index][1..].iter().map(|x| *x).collect() + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_119() { + assert_eq!(vec![1, 3, 3, 1], Solution::get_row(3)); + assert_eq!(vec![1], Solution::get_row(0)); + assert_eq!(vec![1, 1], Solution::get_row(1)); + } +}