summaryrefslogtreecommitdiff
path: root/pascals-triangle/src
diff options
context:
space:
mode:
Diffstat (limited to 'pascals-triangle/src')
-rw-r--r--pascals-triangle/src/main.rs32
1 files changed, 32 insertions, 0 deletions
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<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
+ }
+}
+