Given a string
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
Output: 7
Output: 1
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.
Contents
Solution 1 - Using a stack and loop over digits and operands in input string
In this approach, we will use a
Implementation steps:
-
We will create two variables
number to capture digits as we loop through strings , andsign to track the operator. We will initializesign = '+' , the reason we do this is because, say for example input string is"10-2*3+5/2" for number10 in the beginning it wil default to+ sign. -
We will also create a
Stack for tracking current operands as we will loop through input string. -
Then, we will loop through characters of input string
s :
If the character is digit 0 -9 :
-
Read the character and add it to the
number , when adding a new character into number we need ensure that the position of the numebr is correct. We can do it as follows, multiply current numebr with10 and add new digit, for example the number is"79" , first we will read7 , then when adding we will do it as7*10 + 9 that converts string "79" to integer79 .number = number *10 + (current - '0'); If you dont want to do it this way, you can keep appending the number as string and when it's needed to convert to integer, use Integer.parseInt method.
If the character is an operator or the ending character of input:
-
If the sign is
+ , then add thenumber to thestack . -
If the sign is
- , then add the-number to thestack . -
If the sign is
* , then take the last number from thestack and multiply withnumber and add the result tostack . -
If the sign is
/ , then take the last number from thestack and divide bynumber and add the result tostack . -
Once added to the stack, update the
sign variable with current sign and reset thenumber . -
Now the stack contains numbers after applying
* and/ operations from above steps, now we can simply loop over the stack and return the sum of the numbers.
Example:
Let's take an example
-
First initialize the variables
number = 0 ,sign = '+' , and an emptyStack . -
Then, loop through characters of input string:
-
1st character is
1 , we will append this to numbernumber = number * 10 + ('1' -'0') , this will becomenumber = 0*10 + 1-0 = 1 . -
2nd character is
0 , we will append this to number, it will becomenumber = 1 *10 + 0-0 = 10 . -
3rd character is an operator
- , so we will check the current value thatsign holding, which is+ , so we add current number10 to thestack and updatesign = '-' and resetnumber = 0 .stack 10 -
4th character is
2 , so we will add it tonumber , it will become 2. -
5th character is an operator
* , so we will check the current value thatsign holding, which is- , so we add current number with - sign-2 to thestack and updatesign = '*' and resetnumber = 0 .stack -2 10 -
6th character is
3 , so we will add it tonumber , it will become 3. -
7th character is an operator
+ , so we will check the current value thatsign holding, which is* , so we take latest from stack, multiply with current number, and add it back tostack . So, top number fromstack is-2 and current number is3 , so we will add-6 to the stack and updatesign = '+' and resetnumber = 0 .stack -6 10 -
8th character is
5 , so we will add it tonumber , it will become 5. -
9th character is an operator
/ , so we will check the current value thatsign holding, which is+ , so we will add5 to the stack and updatesign = '/' and resetnumber = 0 .stack 5 -6 10 -
10th character is the last character
2 , so we take latest from stack, divide by current number, and add it back tostack . So, top number fromstack is5 and current number is2 , so we will add5 /2 = 2 to the stack.stack 2 -6 10
-
1st character is
-
Now, we will go through the
stack and add them up,2 - 6 + 10 = 6 , that is our answer.
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n) for the
Above implementations source code can be found at