Given a string s which represents an expression, evaluate this expression and return its value.&nbsp;

The integer division should truncate toward zero.

You may assume that the given expression is always valid. All intermediate results will be in the range of [-231, 231 - 1].

Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().

&nbsp;
Example 1:
Input: s = "3+2*2"
Output: 7
Example 2:
Input: s = " 3/2 "
Output: 1
Example 3:
Input: s = " 3+5 / 2 "
Output: 5

&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 3 * 105
	s consists of integers and operators ('+', '-', '*', '/') separated by some number of spaces.
	s represents a valid expression.
	All the integers in the expression are non-negative integers in the range [0, 231 - 1].
	The answer is guaranteed to fit in a 32-bit integer.


class Solution:
    def calculate(self, s: str) -> int:
        stack = []
        currentNumber = 0
        operator = '+'
        operations = '+-/*'
        for i in range(len(s)):
            ch = s[i]
            if ch.isdigit():
                currentNumber = currentNumber * 10 + int(ch)

            if ch in operations or i == len(s) - 1:
                if operator == '+':
                    stack.append(currentNumber)

                elif operator == '-':
                    stack.append(-currentNumber)

                elif operator == '*':
                    stack.append(stack.pop() * currentNumber)

                elif operator == '/':
                    stack.append(int(stack.pop()/currentNumber))

                currentNumber =0
                operator = ch

        return sum(stack)
class Solution {
    public int calculate(String s) {
        if(s==null ||s.length()==0)return 0;
        Stack<Integer> st = new Stack<>();
        int curr=0;
        char op = '+';
        char [] ch = s.toCharArray();
        for(int i=0;i<s.length();i++){
            if(Character.isDigit(ch[i])){
                curr= curr*10+ch[i]-'0';
            }
            if(!Character.isDigit(ch[i])&& ch[i]!=' ' || i==ch.length-1){
                if(op=='+'){
                    st.push(curr);
                }else if(op=='-'){
                    st.push(-curr);
                }else if(op=='*'){
                    st.push(st.pop()*curr);
                }else if(op=='/'){
                    st.push(st.pop()/curr);
                }
                op=ch[i];
                curr=0;
            }
        }
        int sum=0;
        while(!st.isEmpty()){
            sum+=st.pop();
        }
        return sum;
    }
} //TC=o(n),SC=o(n)
class Solution {
public:
    int calculate(string s) {
        stack<int> nums;
        stack<char> ops;
        int n = 0;
        for(int i = 0; i < s.size(); ++i){
            if(s[i] >= '0' && s[i] <= '9'){
                string t(1, s[i]);
                n = n*10 + stoi(t);
            }else if( s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/'){
                nums.push(n);
                n = 0;
                if(!ops.empty() && (ops.top() == '*' || ops.top() == '/') ){
                    int n2 = nums.top(); nums.pop();
                    int n1 = nums.top(); nums.pop();
                    if(ops.top() == '*') nums.push(n1*n2);
                    else nums.push(n1/n2);
                    ops.pop();
                }
                ops.push(s[i]);
            }
        }
        nums.push(n);
        if(!ops.empty() && (ops.top() == '*' || ops.top() == '/') ){
            int n2 = nums.top(); nums.pop();
            int n1 = nums.top(); nums.pop();
            if(ops.top() == '*') nums.push(n1*n2);
            else nums.push(n1/n2);
            ops.pop();
        }
        stack<int> tnums;
        stack<char> tops;
        while(!nums.empty()){ tnums.push(nums.top()); nums.pop();}
        while(!ops.empty()) { tops.push(ops.top()); ops.pop(); }
        
        while(!tops.empty()){ //cout<<tops.top()<<" " ;
            int n1 = tnums.top(); tnums.pop();
            int n2 = tnums.top(); tnums.pop();
            if(tops.top() == '+') tnums.push(n1 + n2);
            else tnums.push(n1 - n2);
            tops.pop();
        }
        return tnums.top();
    }
};
/**
 * @param {string} s
 * @return {number}
 */
var calculate = function(s) {
    const n = s.length;
    let currNum = 0, lastNum = 0, res = 0;
    let op = '+';
    
    for (let i = 0; i < n; i++) {
        let currChar = s[i];
        
        if (currChar !== " " && !isNaN(Number(currChar))) {
            currNum = currNum * 10 + Number(currChar);
        }
        
        if (isNaN(Number(currChar)) && currChar !== " " || i === n - 1) {
            if (op === '+' || op === '-') {
                res += lastNum;
                lastNum = (op === '+' ? currNum : -currNum);
            } else if (op === '*') {
                lastNum *= currNum;
            } else if (op === '/') {
                lastNum = Math.floor(Math.abs(lastNum) / currNum) * (lastNum < 0 ? -1 : 1);
            }
            
            op = currChar;
            currNum = 0;
        }
    }
    
    res += lastNum;
    return res;
};
Basic Calculator II