File tree 4 files changed +87
-28
lines changed
solution/0100-0199/0118.Pascal's Triangle
4 files changed +87
-28
lines changed Original file line number Diff line number Diff line change 27
27
28
28
<!-- 这里可写通用的实现逻辑 -->
29
29
30
+ 先设置每一行首尾元素为 1,其它元素为 0。然后根据杨辉三角,设置每一行其它元素即可。
31
+
30
32
<!-- tabs:start -->
31
33
32
34
### ** Python3**
33
35
34
36
<!-- 这里可写当前语言的特殊实现逻辑 -->
35
37
36
38
``` python
37
-
39
+ class Solution :
40
+ def generate (self , numRows : int ) -> List[List[int ]]:
41
+ if numRows == 0 :
42
+ return []
43
+ res = []
44
+ for i in range (numRows):
45
+ t = [1 if j == 0 or j == i else 0 for j in range (i + 1 )]
46
+ for j in range (1 , i):
47
+ t[j] = res[i - 1 ][j - 1 ] + res[i - 1 ][j]
48
+ res.append(t)
49
+ return res
38
50
```
39
51
40
52
### ** Java**
41
53
42
54
<!-- 这里可写当前语言的特殊实现逻辑 -->
43
55
44
56
``` java
45
-
57
+ class Solution {
58
+ public List<List<Integer > > generate (int numRows ) {
59
+ List<List<Integer > > res = new ArrayList<> ();
60
+ if (numRows == 0 ) return res;
61
+ for (int i = 0 ; i < numRows; ++ i) {
62
+ // 每一行
63
+ List<Integer > t = new ArrayList<> ();
64
+ for (int j = 0 ; j < i + 1 ; ++ j) {
65
+ boolean firstOrLast = j == 0 || j == i;
66
+ // 设置每一行首尾元素为1,其它元素为0
67
+ t. add(firstOrLast ? 1 : 0 );
68
+ }
69
+ for (int j = 1 ; j < i; ++ j) {
70
+ int val = res. get(i - 1 ). get(j - 1 ) + res. get(i - 1 ). get(j);
71
+ t. set(j, val);
72
+ }
73
+ res. add(t);
74
+ }
75
+ return res;
76
+ }
77
+ }
46
78
```
47
79
48
80
### ** ...**
Original file line number Diff line number Diff line change 41
41
### ** Python3**
42
42
43
43
``` python
44
-
44
+ class Solution :
45
+ def generate (self , numRows : int ) -> List[List[int ]]:
46
+ if numRows == 0 :
47
+ return []
48
+ res = []
49
+ for i in range (numRows):
50
+ t = [1 if j == 0 or j == i else 0 for j in range (i + 1 )]
51
+ for j in range (1 , i):
52
+ t[j] = res[i - 1 ][j - 1 ] + res[i - 1 ][j]
53
+ res.append(t)
54
+ return res
45
55
```
46
56
47
57
### ** Java**
48
58
49
59
``` java
50
-
60
+ class Solution {
61
+ public List<List<Integer > > generate (int numRows ) {
62
+ List<List<Integer > > res = new ArrayList<> ();
63
+ if (numRows == 0 ) return res;
64
+ for (int i = 0 ; i < numRows; ++ i) {
65
+ // 每一行
66
+ List<Integer > t = new ArrayList<> ();
67
+ for (int j = 0 ; j < i + 1 ; ++ j) {
68
+ boolean firstOrLast = j == 0 || j == i;
69
+ // 设置每一行首尾元素为1,其它元素为0
70
+ t. add(firstOrLast ? 1 : 0 );
71
+ }
72
+ for (int j = 1 ; j < i; ++ j) {
73
+ int val = res. get(i - 1 ). get(j - 1 ) + res. get(i - 1 ). get(j);
74
+ t. set(j, val);
75
+ }
76
+ res. add(t);
77
+ }
78
+ return res;
79
+ }
80
+ }
51
81
```
52
82
53
83
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public List <List <Integer >> generate (int numRows ) {
3
- List <List <Integer >> re = new ArrayList <>();
4
- if (numRows == 0 ) return re ;
5
- re .add (Collections .singletonList (1 ));
6
- for (int i = 1 ; i < numRows ; i ++) {
7
- List <Integer > list = new ArrayList <>();
8
- re .add (list );
9
- for (int j = 0 ; j < i + 1 ; j ++) {
10
- int l = j - 1 < 0 ? 0 : re .get (i - 1 ).get (j - 1 );
11
- int r = j > i - 1 ? 0 : re .get (i - 1 ).get (j );
12
- list .add (l + r );
3
+ List <List <Integer >> res = new ArrayList <>();
4
+ if (numRows == 0 ) return res ;
5
+ for (int i = 0 ; i < numRows ; ++i ) {
6
+ // 每一行
7
+ List <Integer > t = new ArrayList <>();
8
+ for (int j = 0 ; j < i + 1 ; ++j ) {
9
+ boolean firstOrLast = j == 0 || j == i ;
10
+ // 设置每一行首尾元素为1,其它元素为0
11
+ t .add (firstOrLast ? 1 : 0 );
13
12
}
13
+ for (int j = 1 ; j < i ; ++j ) {
14
+ int val = res .get (i - 1 ).get (j - 1 ) + res .get (i - 1 ).get (j );
15
+ t .set (j , val );
16
+ }
17
+ res .add (t );
14
18
}
15
- return re ;
19
+ return res ;
16
20
}
17
21
}
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
- res = [[1 ], [1 , 1 ]]
4
3
if numRows == 0 :
5
4
return []
6
- elif numRows == 1 :
7
- return [res [0 ]]
8
-
9
- # from the 3. row on
10
- for i in range (2 , numRows ):
11
- counter = 1
12
- temp = [1 , 1 ]
13
- # should add len(res[i - 1]) - 1 elements in to temp
14
- for j in range (len (res [i - 1 ]) - 1 ):
15
- temp .insert (counter , res [i - 1 ][j ] + res [i - 1 ][j + 1 ])
16
- counter += 1
17
- res .append (temp )
5
+ res = []
6
+ for i in range (numRows ):
7
+ t = [1 if j == 0 or j == i else 0 for j in range (i + 1 )]
8
+ for j in range (1 , i ):
9
+ t [j ] = res [i - 1 ][j - 1 ] + res [i - 1 ][j ]
10
+ res .append (t )
18
11
return res
You can’t perform that action at this time.
0 commit comments