Skip to content

Commit 17b5429

Browse files
author
lucifer
committed
feat: $227
1 parent 8ac8d80 commit 17b5429

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

problems/227.basic-calculator-ii.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ class Solution:
212212
pre_flag = c
213213
num = 0
214214
i += 1
215+
print(sum(stack))
215216
return i, sum(stack)
216217
s += '$'
217218
return dfs(s, 0)[1]
@@ -223,6 +224,21 @@ class Solution:
223224
- 时间复杂度:$O(N)$
224225
- 空间复杂度:$O(N)$
225226

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+
226242
大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 38K star 啦。
227243
大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
228244
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

0 commit comments

Comments
 (0)