forked from KnowledgeCenterYoutube/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
119_Pascal's_Triangle
46 lines (36 loc) · 1.02 KB
/
119_Pascal's_Triangle
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
Leetcode 119: Pascal's Triangle II
Detailed video explanation: https://youtu.be/IWXZAvBIuyE
=========================================================
C++:
----
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> result(rowIndex+1, 1);
for(int i = 1; i < rowIndex; ++i){
for(int j = i; j > 0; --j)
result[j] += result[j-1];
}
return result;
}
};
Java:
-----
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> result = new ArrayList();
for(int i = 0; i < rowIndex+1; ++i) result.add(1);
for(int i = 1; i < rowIndex; ++i)
for(int j = i; j > 0; --j) result.set(j, result.get(j) + result.get(j-1));
return result;
}
}
Python3:
-------
class Solution:
def getRow(self, rowIndex: int) -> List[int]:
result = [1]*(rowIndex + 1)
for i in range(1, rowIndex):
for j in range(i, 0, -1):
result[j] += result[j-1]
return result