File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -212,6 +212,7 @@ class Solution:
212
212
pre_flag = c
213
213
num = 0
214
214
i += 1
215
+ print (sum (stack))
215
216
return i, sum (stack)
216
217
s += ' $'
217
218
return dfs(s, 0 )[1 ]
@@ -223,6 +224,21 @@ class Solution:
223
224
- 时间复杂度:$O(N)$
224
225
- 空间复杂度:$O(N)$
225
226
227
+ 补充:一些同学反映:思路和我的一样,代码也类似,为什么执行不正确?这里我强调一点:
228
+
229
+ - 注意语句 ` if c == ')': break ` 的位置。如果放在其他位置,需要在其前手动增加语句,代码类似:
230
+
231
+ ``` py
232
+ if c == ' )' :
233
+ if pre_flag == ' +' :
234
+ stack.append(num)
235
+ elif pre_flag == ' -' :
236
+ stack.append(- num)
237
+ break
238
+ ```
239
+
240
+ 以 "(1+(4+5+2)-3)+(6+8)" 来说,(4+5+2) 加起来就是 11,如果 break 前不执行上面的语句就会漏掉 2 变成 了 9,而不是 11。
241
+
226
242
大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 38K star 啦。
227
243
大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
228
244
![ ] ( https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg )
You can’t perform that action at this time.
0 commit comments