File tree 6 files changed +101
-30
lines changed
solution/0100-0199/0118.Pascal's Triangle
6 files changed +101
-30
lines changed Original file line number Diff line number Diff line change 40
40
``` python
41
41
class Solution :
42
42
def generate (self , numRows : int ) -> List[List[int ]]:
43
- if numRows == 0 :
44
- return []
45
43
res = []
46
44
for i in range (numRows):
47
45
t = [1 if j == 0 or j == i else 0 for j in range (i + 1 )]
@@ -59,13 +57,10 @@ class Solution:
59
57
class Solution {
60
58
public List<List<Integer > > generate (int numRows ) {
61
59
List<List<Integer > > res = new ArrayList<> ();
62
- if (numRows == 0 ) return res;
63
60
for (int i = 0 ; i < numRows; ++ i) {
64
- // 每一行
65
61
List<Integer > t = new ArrayList<> ();
66
62
for (int j = 0 ; j < i + 1 ; ++ j) {
67
63
boolean firstOrLast = j == 0 || j == i;
68
- // 设置每一行首尾元素为1,其它元素为0
69
64
t. add(firstOrLast ? 1 : 0 );
70
65
}
71
66
for (int j = 1 ; j < i; ++ j) {
@@ -79,6 +74,45 @@ class Solution {
79
74
}
80
75
```
81
76
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
+
82
116
### ** JavaScript**
83
117
84
118
``` js
Original file line number Diff line number Diff line change 33
33
``` python
34
34
class Solution :
35
35
def generate (self , numRows : int ) -> List[List[int ]]:
36
- if numRows == 0 :
37
- return []
38
36
res = []
39
37
for i in range (numRows):
40
38
t = [1 if j == 0 or j == i else 0 for j in range (i + 1 )]
@@ -50,13 +48,10 @@ class Solution:
50
48
class Solution {
51
49
public List<List<Integer > > generate (int numRows ) {
52
50
List<List<Integer > > res = new ArrayList<> ();
53
- if (numRows == 0 ) return res;
54
51
for (int i = 0 ; i < numRows; ++ i) {
55
- // 每一行
56
52
List<Integer > t = new ArrayList<> ();
57
53
for (int j = 0 ; j < i + 1 ; ++ j) {
58
54
boolean firstOrLast = j == 0 || j == i;
59
- // 设置每一行首尾元素为1,其它元素为0
60
55
t. add(firstOrLast ? 1 : 0 );
61
56
}
62
57
for (int j = 1 ; j < i; ++ j) {
@@ -70,6 +65,45 @@ class Solution {
70
65
}
71
66
```
72
67
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
+
73
107
### ** JavaScript**
74
108
75
109
``` js
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
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];
16
11
}
17
- ans .push_back (tmp );
18
- }
19
- return ans;
12
+ res .push_back (t );
13
+ }
14
+ return res;
20
15
}
21
16
};
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public List <List <Integer >> generate (int numRows ) {
3
3
List <List <Integer >> res = new ArrayList <>();
4
- if (numRows == 0 ) return res ;
5
4
for (int i = 0 ; i < numRows ; ++i ) {
6
- // 每一行
7
5
List <Integer > t = new ArrayList <>();
8
6
for (int j = 0 ; j < i + 1 ; ++j ) {
9
7
boolean firstOrLast = j == 0 || j == i ;
10
- // 设置每一行首尾元素为1,其它元素为0
11
8
t .add (firstOrLast ? 1 : 0 );
12
9
}
13
10
for (int j = 1 ; j < i ; ++j ) {
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def generate (self , numRows : int ) -> List [List [int ]]:
3
- if numRows == 0 :
4
- return []
5
3
res = []
6
4
for i in range (numRows ):
7
5
t = [1 if j == 0 or j == i else 0 for j in range (i + 1 )]
You can’t perform that action at this time.
0 commit comments