Skip to content

Commit a169cd2

Browse files
committed
feat: add solutions to lc problem: No.0118. Pascals Triangle
1 parent 00bcff0 commit a169cd2

File tree

6 files changed

+101
-30
lines changed

6 files changed

+101
-30
lines changed

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

+39-5
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
```python
4141
class Solution:
4242
def generate(self, numRows: int) -> List[List[int]]:
43-
if numRows == 0:
44-
return []
4543
res = []
4644
for i in range(numRows):
4745
t = [1 if j == 0 or j == i else 0 for j in range(i + 1)]
@@ -59,13 +57,10 @@ class Solution:
5957
class Solution {
6058
public List<List<Integer>> generate(int numRows) {
6159
List<List<Integer>> res = new ArrayList<>();
62-
if (numRows == 0) return res;
6360
for (int i = 0; i < numRows; ++i) {
64-
// 每一行
6561
List<Integer> t = new ArrayList<>();
6662
for (int j = 0; j < i + 1; ++j) {
6763
boolean firstOrLast = j == 0 || j == i;
68-
// 设置每一行首尾元素为1,其它元素为0
6964
t.add(firstOrLast ? 1 : 0);
7065
}
7166
for (int j = 1; j < i; ++j) {
@@ -79,6 +74,45 @@ class Solution {
7974
}
8075
```
8176

77+
### **C++**
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
vector<vector<int>> generate(int numRows) {
83+
vector<vector<int>> res;
84+
for (int i = 0; i < numRows; ++i) {
85+
vector<int> t(i + 1);
86+
t[0] = 1;
87+
t[i] = 1;
88+
for (int j = 1; j < i; ++j) {
89+
t[j] = res[i - 1][j - 1] + res[i - 1][j];
90+
}
91+
res.push_back(t);
92+
}
93+
return res;
94+
}
95+
};
96+
```
97+
98+
### **Go**
99+
100+
```go
101+
func generate(numRows int) [][]int {
102+
res := make([][]int, numRows)
103+
for i := 0; i < numRows; i++ {
104+
t := make([]int, i+1)
105+
t[0] = 1
106+
t[i] = 1
107+
for j := 1; j < i; j++ {
108+
t[j] = res[i-1][j-1] + res[i-1][j]
109+
}
110+
res[i] = t
111+
}
112+
return res
113+
}
114+
```
115+
82116
### **JavaScript**
83117

84118
```js

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

+39-5
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
```python
3434
class Solution:
3535
def generate(self, numRows: int) -> List[List[int]]:
36-
if numRows == 0:
37-
return []
3836
res = []
3937
for i in range(numRows):
4038
t = [1 if j == 0 or j == i else 0 for j in range(i + 1)]
@@ -50,13 +48,10 @@ class Solution:
5048
class Solution {
5149
public List<List<Integer>> generate(int numRows) {
5250
List<List<Integer>> res = new ArrayList<>();
53-
if (numRows == 0) return res;
5451
for (int i = 0; i < numRows; ++i) {
55-
// 每一行
5652
List<Integer> t = new ArrayList<>();
5753
for (int j = 0; j < i + 1; ++j) {
5854
boolean firstOrLast = j == 0 || j == i;
59-
// 设置每一行首尾元素为1,其它元素为0
6055
t.add(firstOrLast ? 1 : 0);
6156
}
6257
for (int j = 1; j < i; ++j) {
@@ -70,6 +65,45 @@ class Solution {
7065
}
7166
```
7267

68+
### **C++**
69+
70+
```cpp
71+
class Solution {
72+
public:
73+
vector<vector<int>> generate(int numRows) {
74+
vector<vector<int>> res;
75+
for (int i = 0; i < numRows; ++i) {
76+
vector<int> t(i + 1);
77+
t[0] = 1;
78+
t[i] = 1;
79+
for (int j = 1; j < i; ++j) {
80+
t[j] = res[i - 1][j - 1] + res[i - 1][j];
81+
}
82+
res.push_back(t);
83+
}
84+
return res;
85+
}
86+
};
87+
```
88+
89+
### **Go**
90+
91+
```go
92+
func generate(numRows int) [][]int {
93+
res := make([][]int, numRows)
94+
for i := 0; i < numRows; i++ {
95+
t := make([]int, i+1)
96+
t[0] = 1
97+
t[i] = 1
98+
for j := 1; j < i; j++ {
99+
t[j] = res[i-1][j-1] + res[i-1][j]
100+
}
101+
res[i] = t
102+
}
103+
return res
104+
}
105+
```
106+
73107
### **JavaScript**
74108

75109
```js
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
class Solution {
22
public:
33
vector<vector<int>> generate(int numRows) {
4-
vector<vector<int>> ans;
5-
6-
for(int i = 0;i<numRows;i++){
7-
vector<int> tmp(i+1);
8-
tmp[0] = 1;//最左侧为1
9-
for(int j = 1;j<=i;j++){
10-
if(i == j)//最右侧为1
11-
{
12-
tmp[j] = 1;
13-
break;
14-
}
15-
tmp[j] = ans[i-1][j-1] + ans[i-1][j];
4+
vector<vector<int>> res;
5+
for (int i = 0; i < numRows; ++i) {
6+
vector<int> t(i + 1);
7+
t[0] = 1;
8+
t[i] = 1;
9+
for (int j = 1; j < i; ++j) {
10+
t[j] = res[i - 1][j - 1] + res[i - 1][j];
1611
}
17-
ans.push_back(tmp);
18-
}
19-
return ans;
12+
res.push_back(t);
13+
}
14+
return res;
2015
}
2116
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func generate(numRows int) [][]int {
2+
res := make([][]int, numRows)
3+
for i := 0; i < numRows; i++ {
4+
t := make([]int, i+1)
5+
t[0] = 1
6+
t[i] = 1
7+
for j := 1; j < i; j++ {
8+
t[j] = res[i-1][j-1] + res[i-1][j]
9+
}
10+
res[i] = t
11+
}
12+
return res
13+
}

solution/0100-0199/0118.Pascal's Triangle/Solution.java

-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
class Solution {
22
public List<List<Integer>> generate(int numRows) {
33
List<List<Integer>> res = new ArrayList<>();
4-
if (numRows == 0) return res;
54
for (int i = 0; i < numRows; ++i) {
6-
// 每一行
75
List<Integer> t = new ArrayList<>();
86
for (int j = 0; j < i + 1; ++j) {
97
boolean firstOrLast = j == 0 || j == i;
10-
// 设置每一行首尾元素为1,其它元素为0
118
t.add(firstOrLast ? 1 : 0);
129
}
1310
for (int j = 1; j < i; ++j) {

solution/0100-0199/0118.Pascal's Triangle/Solution.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
class Solution:
22
def generate(self, numRows: int) -> List[List[int]]:
3-
if numRows == 0:
4-
return []
53
res = []
64
for i in range(numRows):
75
t = [1 if j == 0 or j == i else 0 for j in range(i + 1)]

0 commit comments

Comments
 (0)