Skip to content

Commit a71a1e0

Browse files
committed
'Pascal's Triangle' soln
1 parent cb74700 commit a71a1e0

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ Note: Some solutions have multiple approaches implemented
121121
| 113 | [Path Sum II](https://leetcode.com/problems/path-sum-ii/) | [Solution](./leetcode/path_sum_ii.rs) | Medium |
122122
| 114 | [Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | [Solution](./leetcode/flatten_binary_tree.rs) | Medium |
123123
| 115 | [Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/) | [Solution](./leetcode/distinct_subsequences.rs) | Hard |
124-
| >116 | [Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/) | [Solution](./leetcode/populating_next_right_pointers.rs) | Medium |
125-
| 117 | [Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/) | [Solution](./leetcode/populating_next_right_pointers_ii.rs) | Medium |
124+
| x116 | [Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/) | [Solution](./leetcode/populating_next_right_pointers.rs) | Medium |
125+
| x117 | [Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/) | [Solution](./leetcode/populating_next_right_pointers_ii.rs) | Medium |
126126
| 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) | [Solution](./leetcode/pascals_triangle.rs) | Easy |
127127
| 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/) | [Solution](./leetcode/pascals_triangle_ii.rs) | Easy |
128128
| 120 | [Triangle](https://leetcode.com/problems/triangle/) | [Solution](./leetcode/triangle.rs) | Medium |

leetcode/pascals_triangle.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
///
2+
/// Problem: Pascal's Triangle
3+
///
4+
/// Given an integer numRows, return the first numRows of Pascal's triangle.
5+
///
6+
/// In Pascal's triangle, each number is the sum of the two numbers directly above it.
7+
///
8+
/// Example 1:
9+
/// Input: numRows = 5
10+
/// Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
11+
///
12+
/// Example 2:
13+
/// Input: numRows = 1
14+
/// Output: [[1]]
15+
///
16+
/// Constraints:
17+
/// 1 <= numRows <= 30
18+
///
19+
20+
// # Solution:
21+
// Time complexity: O(numRows²)
22+
// Space complexity: O(numRows²)
23+
24+
impl Solution {
25+
pub fn generate(num_rows: i32) -> Vec<Vec<i32>> {
26+
let num_rows = num_rows as usize;
27+
let mut result = Vec::with_capacity(num_rows);
28+
29+
30+
result.push(vec![1]);
31+
32+
33+
for i in 1..num_rows {
34+
let prev_row = &result[i - 1];
35+
let mut current_row = Vec::with_capacity(i + 1);
36+
37+
38+
current_row.push(1);
39+
40+
41+
for j in 1..i {
42+
current_row.push(prev_row[j - 1] + prev_row[j]);
43+
}
44+
45+
46+
current_row.push(1);
47+
48+
result.push(current_row);
49+
}
50+
51+
result
52+
}
53+
}

0 commit comments

Comments
 (0)