304 North Cardinal St.
Dorchester Center, MA 02124

# Basic Calculator II LeetCode Solution

## Problem – Basic Calculator II

Given a string `s` which represents an expression, evaluate this expression and return its value

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()`.

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

Constraints:

• `1 <= s.length <= 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.

### Basic Calculator II LeetCode Solution in Java

``````public class Solution {
public int calculate(String s) {
int len;
if(s==null || (len = s.length())==0) return 0;
Stack<Integer> stack = new Stack<Integer>();
int num = 0;
char sign = '+';
for(int i=0;i<len;i++){
if(Character.isDigit(s.charAt(i))){
num = num*10+s.charAt(i)-'0';
}
if((!Character.isDigit(s.charAt(i)) &&' '!=s.charAt(i)) || i==len-1){
if(sign=='-'){
stack.push(-num);
}
if(sign=='+'){
stack.push(num);
}
if(sign=='*'){
stack.push(stack.pop()*num);
}
if(sign=='/'){
stack.push(stack.pop()/num);
}
sign = s.charAt(i);
num = 0;
}
}

int re = 0;
for(int i:stack){
re += i;
}
return re;
}
``````

### Basic Calculator II LeetCode Solution in C++

``````int calculate(string s) {
istringstream in('+' + s + '+');
long long total = 0, term = 0, n;
char op;
while (in >> op) {
if (op == '+' or op == '-') {
total += term;
in >> term;
term *= 44 - op;
} else {
in >> n;
if (op == '*')
term *= n;
else
term /= n;
}
}
}
``````

### Basic Calculator II LeetCode Solution in Python

``````def calculate(self, s):
if not s:
return "0"
stack, num, sign = [], 0, "+"
for i in xrange(len(s)):
if s[i].isdigit():
num = num*10+ord(s[i])-ord("0")
if (not s[i].isdigit() and not s[i].isspace()) or i == len(s)-1:
if sign == "-":
stack.append(-num)
elif sign == "+":
stack.append(num)
elif sign == "*":
stack.append(stack.pop()*num)
else:
tmp = stack.pop()
if tmp//num < 0 and tmp%num != 0:
stack.append(tmp//num+1)
else:
stack.append(tmp//num)
sign = s[i]
num = 0
return sum(stack)``````
##### Basic Calculator II LeetCode Solution Review:

In our experience, we suggest you solve this Basic Calculator II LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Basic Calculator II LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Basic Calculator II LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions