Skip to content

Commit a99f82b

Browse files
committed
update readme
1 parent ffaccd0 commit a99f82b

File tree

5 files changed

+29
-109
lines changed

5 files changed

+29
-109
lines changed

0227-basic-calculator-ii/0227-basic-calculator-ii.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

0227-basic-calculator-ii/README.md

Lines changed: 0 additions & 29 deletions
This file was deleted.

Python/0227-basic-calculator-ii.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
# time complexity: O(n)
22
# space complexity: O(n)
3-
class Solution:
3+
import math
4+
45

6+
class Solution:
57
def calculate(self, s: str) -> int:
6-
if len(s) == 0:
7-
return 0
8+
num = 0
9+
prevOperation = '+'
10+
s += '+'
811
stack = []
9-
currNum = 0
10-
operation = "+"
11-
for i in range(len(s)):
12-
c = s[i]
12+
for c in s:
1313
if c.isdigit():
14-
currNum = currNum * 10 + int(c)
15-
if (not c.isdigit() and not s[i].isspace()) or i == len(s) - 1:
16-
if operation == "-":
17-
stack.append(-currNum)
18-
elif operation == "*":
19-
stack.append(stack.pop() * currNum)
20-
elif operation == "+":
21-
stack.append(currNum)
22-
else:
23-
tmp = stack.pop()
24-
if tmp // currNum < 0 and tmp % currNum != 0:
25-
stack.append(tmp // currNum + 1)
26-
else:
27-
stack.append(tmp // currNum)
28-
operation = s[i]
29-
currNum = 0
14+
num = num*10 + int(c)
15+
elif c == ' ':
16+
pass
17+
else:
18+
if prevOperation == '+':
19+
stack.append(num)
20+
elif prevOperation == '-':
21+
stack.append(-num)
22+
elif prevOperation == '*':
23+
operant = stack.pop()
24+
stack.append((operant*num))
25+
elif prevOperation == '/':
26+
operant = stack.pop()
27+
stack.append(math.trunc(operant/num))
28+
num = 0
29+
prevOperation = c
3030
return sum(stack)
3131

3232

3333
s = "3+2*2"
3434
print(Solution().calculate(s))
35+
s = " 3/2 "
36+
print(Solution().calculate(s))
37+
s = " 3+5 / 2 "
38+
print(Solution().calculate(s))

README.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,3 @@ By following these steps, users can easily navigate and utilize your LeetCode so
8181

8282
We welcome contributions! If you have a solution to a problem that isn't already included, or if you have suggestions for improvements, please feel free to submit a pull request.
8383

84-
85-
<!---LeetCode Topics Start-->
86-
# LeetCode Topics
87-
## Math
88-
| |
89-
| ------- |
90-
| [0227-basic-calculator-ii](https://github.com/hogan-tech/leetcode-solution/tree/master/0227-basic-calculator-ii) |
91-
## String
92-
| |
93-
| ------- |
94-
| [0227-basic-calculator-ii](https://github.com/hogan-tech/leetcode-solution/tree/master/0227-basic-calculator-ii) |
95-
## Stack
96-
| |
97-
| ------- |
98-
| [0227-basic-calculator-ii](https://github.com/hogan-tech/leetcode-solution/tree/master/0227-basic-calculator-ii) |
99-
<!---LeetCode Topics End-->

Readme/0227-basic-calculator-ii.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h2><a href="https://leetcode.com/problems/basic-calculator-ii/">227. Basic Calculator II</a></h2><h3>Medium</h3><hr><div><p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>.&nbsp;</p>
1+
<h2><a href="https://leetcode.com/problems/basic-calculator-ii">227. Basic Calculator II</a></h2><h3>Medium</h3><hr><p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>.&nbsp;</p>
22

33
<p>The integer division should truncate toward zero.</p>
44

@@ -22,9 +22,8 @@
2222

2323
<ul>
2424
<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
25-
<li><code>s</code> consists of integers and operators <code>('+', '-', '*', '/')</code> separated by some number of spaces.</li>
25+
<li><code>s</code> consists of integers and operators <code>(&#39;+&#39;, &#39;-&#39;, &#39;*&#39;, &#39;/&#39;)</code> separated by some number of spaces.</li>
2626
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
2727
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
2828
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
2929
</ul>
30-
</div>

0 commit comments

Comments
 (0)