Skip to content

Commit 2fc00b4

Browse files
committed
Sync LeetCode submission Runtime - 71 ms (56.08%), Memory - 18.9 MB (63.13%)
1 parent 4295fce commit 2fc00b4

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

0227-basic-calculator-ii/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>.&nbsp;</p>
2+
3+
<p>The integer division should truncate toward zero.</p>
4+
5+
<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>
6+
7+
<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
<pre><strong>Input:</strong> s = "3+2*2"
12+
<strong>Output:</strong> 7
13+
</pre><p><strong class="example">Example 2:</strong></p>
14+
<pre><strong>Input:</strong> s = " 3/2 "
15+
<strong>Output:</strong> 1
16+
</pre><p><strong class="example">Example 3:</strong></p>
17+
<pre><strong>Input:</strong> s = " 3+5 / 2 "
18+
<strong>Output:</strong> 5
19+
</pre>
20+
<p>&nbsp;</p>
21+
<p><strong>Constraints:</strong></p>
22+
23+
<ul>
24+
<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>(&#39;+&#39;, &#39;-&#39;, &#39;*&#39;, &#39;/&#39;)</code> separated by some number of spaces.</li>
26+
<li><code>s</code> represents <strong>a valid expression</strong>.</li>
27+
<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
28+
<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
29+
</ul>

0227-basic-calculator-ii/solution.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution:
5+
def calculate(self, s: str) -> int:
6+
if not s:
7+
return 0
8+
9+
length = len(s)
10+
current_number = 0
11+
last_number = 0
12+
result = 0
13+
operation = '+'
14+
15+
for i in range(length):
16+
current_char = s[i]
17+
18+
if current_char.isdigit():
19+
current_number = (current_number * 10) + int(current_char)
20+
21+
if (not current_char.isdigit() and not current_char.isspace()) or i == length - 1:
22+
if operation in ['+', '-']:
23+
result += last_number
24+
last_number = current_number if operation == '+' else -current_number
25+
elif operation == '*':
26+
last_number = last_number * current_number
27+
elif operation == '/':
28+
last_number = int(last_number / current_number)
29+
30+
operation = current_char
31+
current_number = 0
32+
33+
result += last_number
34+
return result

0 commit comments

Comments
 (0)