From 0bcf0977cf20d02e09a0b723ef105b2c4d5bf690 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Tue, 31 May 2022 23:24:52 +0100 Subject: [PATCH] pascals-triangle --- pascals-triangle/sol.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 pascals-triangle/sol.rs diff --git a/pascals-triangle/sol.rs b/pascals-triangle/sol.rs new file mode 100644 index 0000000..2e28985 --- /dev/null +++ b/pascals-triangle/sol.rs @@ -0,0 +1,16 @@ +impl Solution { + pub fn generate(num_rows: i32) -> Vec> { + let mut rows = vec![vec![1]]; + for row_idx in (1usize..num_rows as usize) { + // Could do this with .window(2), adding the two elements of each window, + // but that's a bit fussy in this case I think. + let mut row = vec![1]; + for idx in (0usize..(rows[row_idx-1].len()-1)) { + row.push(rows[row_idx-1][idx]+rows[row_idx-1][idx+1]); + } + row.push(1); + rows.push(row); + } + rows + } +} \ No newline at end of file