From 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb Mon Sep 17 00:00:00 2001 From: Orangerot Date: Thu, 27 Jun 2024 11:30:16 +0200 Subject: Initial commit --- pascals-triangle/Cargo.toml | 8 ++++++++ pascals-triangle/src/main.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 pascals-triangle/Cargo.toml create mode 100644 pascals-triangle/src/main.rs (limited to 'pascals-triangle') diff --git a/pascals-triangle/Cargo.toml b/pascals-triangle/Cargo.toml new file mode 100644 index 0000000..99cb41f --- /dev/null +++ b/pascals-triangle/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "pascals-triangle" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/pascals-triangle/src/main.rs b/pascals-triangle/src/main.rs new file mode 100644 index 0000000..7755650 --- /dev/null +++ b/pascals-triangle/src/main.rs @@ -0,0 +1,32 @@ +fn main() { + println!("Hello, world!"); + let tests = vec![ + (5, vec![ + vec![1],vec![1,1],vec![1,2,1],vec![1,3,3,1],vec![1,4,6,4,1] + ]), + (1, vec![vec![1]]) + ]; + + for test in tests { + println!("{} is {:?} should be {:?}", test.0, Solution::generate(test.0), test.1); + } +} + +struct Solution {} +impl Solution { + pub fn generate(num_rows: i32) -> Vec> { + let mut rows = Vec::with_capacity(num_rows as usize); + rows.push(vec![1]); + for i in 1..num_rows { + let mut row: Vec = Vec::with_capacity((i+1) as usize); + for ii in 0..=i { + let above = &rows[(i-1) as usize]; + row.push(if ii == 0 || i == ii {1} else {above[(ii-1) as usize] + above[ii as usize]}); + } + rows.push(row); + } + + rows + } +} + -- cgit v1.2.3