summaryrefslogtreecommitdiff
path: root/pascals-triangle
diff options
context:
space:
mode:
authorOrangerot <purple@orangerot.dev>2024-06-27 11:30:16 +0200
committerOrangerot <purple@orangerot.dev>2024-06-27 11:30:16 +0200
commit4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb (patch)
tree0072cca328fe5adb2ed61004010228ff85e2164d /pascals-triangle
Initial commitHEADmain
Diffstat (limited to 'pascals-triangle')
-rw-r--r--pascals-triangle/Cargo.toml8
-rw-r--r--pascals-triangle/src/main.rs32
2 files changed, 40 insertions, 0 deletions
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<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
+ }
+}
+