summaryrefslogtreecommitdiff
path: root/pascals-triangle/src/main.rs
blob: 77556509627d3f68bc0b40f05d629260ed5591d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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<Vec<i32>> {
        let mut rows = Vec::with_capacity(num_rows as usize);
        rows.push(vec![1]);
        for i in 1..num_rows {
            let mut row: Vec<i32> = 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
    }
}