Skip to content

Commit eac7c55

Browse files
committed
solve problem Pascals Triangle
1 parent 1408c43 commit eac7c55

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ All solutions will be accepted!
5656
|82|[Remove Duplicates From Sorted List II](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/description/)|[java/py/js](./algorithms/RemoveDuplicatesFromSortedListII)|Medium|
5757
|811|[Subdomain Visit Count](https://leetcode-cn.com/problems/subdomain-visit-count/description/)|[java/py/js](./algorithms/SubdomainVisitCount)|Easy|
5858
|637|[Average Of Levels In Binary Tree](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/description/)|[java/py/js](./algorithms/AverageOfLevelsInBinaryTree)|Easy|
59+
|118|[Pascals Triangle](https://leetcode-cn.com/problems/pascals-triangle/description/)|[java/py/js](./algorithms/PascalsTriangle)|Easy|
5960

6061
# Database
6162
|#|Title|Solution|Difficulty|

algorithms/PascalsTriangle/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Pascals Triangle
2+
This problem is easy to solve
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public List<List<Integer>> generate(int numRows) {
3+
List<List<Integer>> res = new ArrayList<List<Integer>>();
4+
for (int i = 0; i < numRows; i++) {
5+
List<Integer> row = new ArrayList<Integer>();
6+
row.add(1);
7+
if (i > 0) {
8+
List<Integer> lastRow = res.get(res.size() - 1);
9+
for (int j = 0; j < lastRow.size(); j++) {
10+
if (j + 1 < lastRow.size()) {
11+
row.add(lastRow.get(j) + lastRow.get(j + 1));
12+
} else {
13+
row.add(lastRow.get(j));
14+
}
15+
}
16+
}
17+
res.add(row);
18+
}
19+
return res;
20+
}
21+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number} numRows
3+
* @return {number[][]}
4+
*/
5+
var generate = function(numRows) {
6+
let res = []
7+
for (let i = 0; i < numRows; i++) {
8+
if (i == 0) {
9+
res.push([1])
10+
} else {
11+
temp = [1]
12+
last = res[res.length - 1]
13+
for (let j = 0; j < last.length; j++) {
14+
if (j + 1 < last.length) {
15+
temp.push(last[j] + last[j + 1])
16+
} else {
17+
temp.push(last[j])
18+
}
19+
}
20+
res.push(temp)
21+
}
22+
}
23+
return res
24+
};
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def generate(self, numRows):
3+
"""
4+
:type numRows: int
5+
:rtype: List[List[int]]
6+
"""
7+
res = []
8+
for i in range(0, numRows):
9+
if len(res) == 0:
10+
res.append([1])
11+
continue
12+
last = res[-1]
13+
temp = [1]
14+
for j in range(0, len(last)):
15+
if j + 1 < len(last):
16+
temp.append(last[j] + last[j + 1])
17+
else:
18+
temp.append(last[j])
19+
res.append(temp)
20+
return res

0 commit comments

Comments
 (0)