forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pascal_triangle.rs
51 lines (45 loc) · 1.23 KB
/
pascal_triangle.rs
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/// ## Paslcal's triangle problem
/// pascal_triangle(num_rows) returns the first num_rows of Pascal's triangle.
/// About Pascal's triangle: https://en.wikipedia.org/wiki/Pascal%27s_triangle
///
/// Arguments:
/// * `num_rows` - number of rows of triangle
/// Complexity
/// - time complexity: O(n^2),
/// - space complexity: O(n^2),
pub fn pascal_triangle(num_rows: i32) -> Vec<Vec<i32>> {
let mut ans: Vec<Vec<i32>> = vec![];
for i in 1..num_rows + 1 {
let mut vec: Vec<i32> = vec![1];
let mut res: i32 = 1;
for k in 1..i {
res *= i - k;
res /= k;
vec.push(res);
}
ans.push(vec);
}
ans
}
#[cfg(test)]
mod tests {
use super::pascal_triangle;
#[test]
fn test() {
assert_eq!(pascal_triangle(3), vec![vec![1], vec![1, 1], vec![1, 2, 1]]);
assert_eq!(
pascal_triangle(4),
vec![vec![1], vec![1, 1], vec![1, 2, 1], vec![1, 3, 3, 1]]
);
assert_eq!(
pascal_triangle(5),
vec![
vec![1],
vec![1, 1],
vec![1, 2, 1],
vec![1, 3, 3, 1],
vec![1, 4, 6, 4, 1]
]
);
}
}