Skip to content

Commit 1f33fb1

Browse files
feat: add solutions to lc problem: No.0119.Pascal's Triangle (doocs#507)
Co-authored-by: MaoLongLong <382084620@qq.com>
1 parent 3874470 commit 1f33fb1

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

solution/0100-0199/0119.Pascal's Triangle II/README.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,59 @@
3434
<!-- 这里可写当前语言的特殊实现逻辑 -->
3535

3636
```python
37-
37+
class Solution:
38+
def getRow(self, rowIndex: int) -> List[int]:
39+
def makePascal(prevArr):
40+
if len(prevArr) == 0:
41+
return [1]
42+
elif len(prevArr) == 1:
43+
return [1, 1]
44+
else:
45+
NewArr = [0] * (len(prevArr) + 1)
46+
NewArr[0], NewArr[-1] = 1, 1
47+
for i in range(len(prevArr) - 1):
48+
NewArr[i + 1] = prevArr[i] + prevArr[i + 1]
49+
return NewArr
50+
51+
temp = []
52+
Pascal = []
53+
for i in range(rowIndex + 1):
54+
temp = makePascal(temp)
55+
Pascal.append(temp)
56+
return Pascal[rowIndex]
3857
```
3958

4059
### **Java**
4160

4261
<!-- 这里可写当前语言的特殊实现逻辑 -->
4362

4463
```java
64+
class Solution {
65+
public List<Integer> getRow(int rowIndex) {
66+
List<Integer> ret = new LinkedList<>();
67+
long nk = 1;
68+
for (int i = 0; i <= rowIndex; i++) {
69+
ret.add((int) nk);
70+
nk = nk * (rowIndex - i) / (i + 1);
71+
}
72+
return ret;
73+
}
74+
}
75+
```
4576

77+
### **Go**
78+
79+
```go
80+
func getRow(rowIndex int) []int {
81+
row := make([]int, rowIndex+1)
82+
row[0] = 1
83+
for i := 1; i <= rowIndex; i++ {
84+
for j := i; j > 0; j-- {
85+
row[j] += row[j-1]
86+
}
87+
}
88+
return row
89+
}
4690
```
4791

4892
### **...**

solution/0100-0199/0119.Pascal's Triangle II/README_EN.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,57 @@
3737
### **Python3**
3838

3939
```python
40-
40+
class Solution:
41+
def getRow(self, rowIndex: int) -> List[int]:
42+
def makePascal(prevArr):
43+
if len(prevArr) == 0:
44+
return [1]
45+
elif len(prevArr) == 1:
46+
return [1, 1]
47+
else:
48+
NewArr = [0] * (len(prevArr) + 1)
49+
NewArr[0], NewArr[-1] = 1, 1
50+
for i in range(len(prevArr) - 1):
51+
NewArr[i + 1] = prevArr[i] + prevArr[i + 1]
52+
return NewArr
53+
54+
temp = []
55+
Pascal = []
56+
for i in range(rowIndex + 1):
57+
temp = makePascal(temp)
58+
Pascal.append(temp)
59+
return Pascal[rowIndex]
4160
```
4261

4362
### **Java**
4463

4564
```java
65+
class Solution {
66+
public List<Integer> getRow(int rowIndex) {
67+
List<Integer> ret = new LinkedList<>();
68+
long nk = 1;
69+
for (int i = 0; i <= rowIndex; i++) {
70+
ret.add((int) nk);
71+
nk = nk * (rowIndex - i) / (i + 1);
72+
}
73+
return ret;
74+
}
75+
}
76+
```
4677

78+
### **Go**
79+
80+
```go
81+
func getRow(rowIndex int) []int {
82+
row := make([]int, rowIndex+1)
83+
row[0] = 1
84+
for i := 1; i <= rowIndex; i++ {
85+
for j := i; j > 0; j-- {
86+
row[j] += row[j-1]
87+
}
88+
}
89+
return row
90+
}
4791
```
4892

4993
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func getRow(rowIndex int) []int {
2+
row := make([]int, rowIndex+1)
3+
row[0] = 1
4+
for i := 1; i <= rowIndex; i++ {
5+
for j := i; j > 0; j-- {
6+
row[j] += row[j-1]
7+
}
8+
}
9+
return row
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def getRow(self, rowIndex: int) -> List[int]:
3+
def makePascal(prevArr):
4+
if len(prevArr) == 0:
5+
return [1]
6+
elif len(prevArr) == 1:
7+
return [1, 1]
8+
else:
9+
NewArr = [0] * (len(prevArr) + 1)
10+
NewArr[0], NewArr[-1] = 1, 1
11+
for i in range(len(prevArr) - 1):
12+
NewArr[i + 1] = prevArr[i] + prevArr[i + 1]
13+
return NewArr
14+
15+
temp = []
16+
Pascal = []
17+
for i in range(rowIndex + 1):
18+
temp = makePascal(temp)
19+
Pascal.append(temp)
20+
return Pascal[rowIndex]

0 commit comments

Comments
 (0)