Skip to content

Commit 190953a

Browse files
authored
Merge pull request #29 from fartem/118_Pascal's_Triangle
2023-02-20 update: added "118. Pascal's Triangle"
2 parents 0d95181 + 5d64153 commit 190953a

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
3737
| 110. Balanced Binary Tree | [Link](https://leetcode.com/problems/balanced-binary-tree/) | [Link](./lib/easy/balanced_binary_tree.dart) |
3838
| 111. Minimum Depth of Binary Tree | [Link](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Link](./lib/easy/minimum_depth_of_binary_tree.dart) |
3939
| 112. Path Sum | [Link](https://leetcode.com/problems/path-sum/) | [Link](./lib/easy/path_sum.dart) |
40+
| 118. Pascal's Triangle | [Link](https://leetcode.com/problems/pascals-triangle/) | [Link](./lib/easy/pascals_triangle.dart) |

lib/easy/pascals_triangle.dart

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://leetcode.com/problems/pascals-triangle/
2+
class Solution {
3+
List<List<int>> generate(int numRows) {
4+
var result = <List<int>>[];
5+
result.add([1]);
6+
for (int i = 1; i < numRows; i++) {
7+
var row = List.generate(i + 1, (_) => 0);
8+
row[0] = 1;
9+
row[row.length - 1] = 1;
10+
if (i >= 2) {
11+
var prev = result[i - 1];
12+
var p = 1;
13+
for (var j = 0; j < prev.length - 1; j++) {
14+
row[p++] = prev[j] + prev[j + 1];
15+
}
16+
}
17+
result.add(row);
18+
}
19+
return result;
20+
}
21+
}

test/easy/pascals_triangle_test.dart

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'package:leetcode_dart/easy/pascals_triangle.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
group(
6+
'Example tests',
7+
() {
8+
final pt = Solution();
9+
test(
10+
'[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]',
11+
() => expect(
12+
[
13+
[1],
14+
[1, 1],
15+
[1, 2, 1],
16+
[1, 3, 3, 1],
17+
[1, 4, 6, 4, 1],
18+
],
19+
pt.generate(5),
20+
),
21+
);
22+
test(
23+
'[1]',
24+
() => expect(
25+
[
26+
[1],
27+
],
28+
pt.generate(1),
29+
),
30+
);
31+
},
32+
);
33+
}

0 commit comments

Comments
 (0)