File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
docs/data-structure/array Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -1087,6 +1087,27 @@ class Solution(object):
1087
1087
return res
1088
1088
```
1089
1089
1090
+ 优化:
1091
+
1092
+ ``` go
1093
+ func generate (numRows int ) [][]int {
1094
+ var res [][]int = make ([][]int , numRows)
1095
+
1096
+ for i := 0 ; i < numRows; i++ {
1097
+ res[i] = make ([]int , i + 1 )
1098
+ for j := 0 ; j < i + 1 ; j++ {
1099
+ if j == 0 || j == i {
1100
+ res[i][j] = 1
1101
+ } else {
1102
+ res[i][j] = res[i - 1 ][j - 1 ] + res[i - 1 ][j]
1103
+ }
1104
+ }
1105
+ }
1106
+
1107
+ return res
1108
+ }
1109
+ ```
1110
+
1090
1111
1091
1112
## 119. 杨辉三角 II
1092
1113
@@ -1124,6 +1145,32 @@ class Solution(object):
1124
1145
return cur
1125
1146
```
1126
1147
1148
+ ### 解二:递归
1149
+
1150
+ ``` python
1151
+ class Solution :
1152
+ def generate (self , numRows : int ) -> List[List[int ]]:
1153
+ ans = []
1154
+ # 递归:输入上一行,返回下一行
1155
+ def helper (pre ):
1156
+ length = len (pre)
1157
+ if length == numRows:
1158
+ return
1159
+ if length == 0 :
1160
+ nxt = [1 ]
1161
+ elif length == 1 :
1162
+ nxt = [1 , 1 ]
1163
+ else :
1164
+ nxt = [1 ]
1165
+ for i in range (length - 1 ):
1166
+ nxt.append(pre[i] + pre[i + 1 ])
1167
+ nxt.append(1 )
1168
+ ans.append(nxt)
1169
+ helper(nxt)
1170
+
1171
+ helper([])
1172
+ return ans
1173
+ ```
1127
1174
1128
1175
## 121. 买卖股票的最佳时机
1129
1176
You can’t perform that action at this time.
0 commit comments