pascals-triangle

This commit is contained in:
Gleb Koval 2022-05-31 23:24:52 +01:00
parent 365cacbd42
commit 0bcf0977cf
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
1 changed files with 16 additions and 0 deletions

16
pascals-triangle/sol.rs Normal file
View File

@ -0,0 +1,16 @@
impl Solution {
pub fn generate(num_rows: i32) -> Vec<Vec<i32>> {
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
}
}