<p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>.&nbsp;</p>

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

<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>

<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>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
	<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>
	<li><code>s</code> represents <strong>a valid expression</strong>.</li>
	<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
	<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>

class Solution:
    def calculate(self, s: str) -> int:
        nums = []
        ops = []
        num = ''  # Change num to a string to accumulate digits
        s = s.replace(" ", "")  # Remove spaces

        # Loop to process the string
        for i in range(len(s)):
            if '0' <= s[i] <= '9':  # If it's a digit, accumulate it in num
                num += s[i]
            else:  # If it's an operator
                nums.append(int(num))  # Append the accumulated number
                num = ''  # Reset num after appending
                ops.append(s[i])  # Append the operator

        nums.append(int(num))  # Append the last accumulated number

        # If there are no operators, return the number itself
        if len(ops) == 0:
            return nums[0]
    
        print("Numbers:", nums)
        print("Operators:", ops)
        
        # First pass: Handle * and / operations
        j = 0
        for i in range(len(ops)):
            if j + 1 <= len(nums) - 1:
                if ops[i] == '*':
                    nums[j:j+2] = [nums[j] * nums[j+1]]
                elif ops[i] == '/':
                    nums[j:j+2] = [int(nums[j] / nums[j+1])] if nums[j+1] != 0 else [0]
                else:
                    j += 1
        
        print("After * and /:", nums)

        # Second pass: Handle + and - operations
        for i in range(len(ops)):
            if ops[i] == '+':
                nums[0:2] = [nums[0] + nums[1]]
            elif ops[i] == '-':
                nums[0:2] = [nums[0] - nums[1]]

        return nums[0]

22400