Topic:
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers,,, +
-
*
, /
operators and empty spaces
. The integer division should truncate toward zero.
Assume that the given expression was always valid.
Some Examples:
"3+2*2" = 7 "3/2" = 1 "3+5/2" = 5
Note:do not use the eval
built-in library function.
Test instructions: Simple calculator, can be arithmetic
Idea: On the basis of I can be very clear analysis of the problem, using two stacks, a storage number, a storage symbol need to pay attention to a few points:
(1) Continuous numbers need to be processed;
(2) Multiplication need to be processed after a number in the stack;
(3) The last stack of the remaining for addition and subtraction, only need a step-by-step pop-up, you need to remember that only pop up a number and symbols, not two numeric operations, in I have a description, forgotten words can be clicked, Basic Calculator I
(4) The last number needs to be treated specially, because the previous method will only use the number as the stack when the symbol is detected.
Code (later with optimized version):
Public classSolution { Public intCalculate (String s) {if(s = =NULL|| S.length () = = 0) return0; Stack<Integer> num =NewStack<integer>(); Stack<Character> symbol =NewStack<character>(); BooleanIsnum =false; intCount = 0; for(inti = 0; I < s.length (); i++){ if(S.charat (i) = = ") Continue; Chartemp =S.charat (i); if(Character.isdigit (temp)) {count= count * + (Temp-' 0 ')) ; Isnum=true; }Else if(isnum) {Num.push (count); Count= 0; Isnum=false; if(!symbol.isempty () && (symbol.peek () = = ' * ' | | symbol.peek () = = '/')){ CharTempsymbol =Symbol.pop (); intb =Num.pop (); intA =Num.pop (); if(Tempsymbol = = ' * ') {Num.push (a*b); }Else{Num.push (a/b); } } } if(!character.isdigit (temp)) {Symbol.push (temp); } } if(isnum) {Num.push (count); if(!symbol.isempty () && (symbol.peek () = = ' * ' | | symbol.peek () = = '/')){ CharTempsymbol =Symbol.pop (); intb =Num.pop (); intA =Num.pop (); if(Tempsymbol = = ' * ') {Num.push (a*b); }Else{Num.push (a/b); } } } if(!Symbol.isempty ()) { intRep = 0; while(!Symbol.isempty ()) { CharTempsymbol =Symbol.pop (); intA =Num.pop (); if(Tempsymbol = = ' + ')) {Rep+=A; }Else{Rep-=A; }} Num.push (Rep+Num.pop ()); } returnNum.pop (); }}
Optimization idea: Notice that the code has redundant parts, because every time the number into the stack is judged signed, at this time, can be artificially at the end of the initial entry and exit of the string to add non-operational symbols, so that the two can be integrated together.
Optimized after Code:
Public classSolution { Public intCalculate (String s) {if(s = =NULL|| S.length () = = 0) return0; Stack<Integer> num =NewStack<integer>(); Stack<Character> symbol =NewStack<character>(); S+= "#"; BooleanIsnum =false; intCount = 0; for(inti = 0; I < s.length (); i++){ if(S.charat (i) = = ") Continue; Chartemp =S.charat (i); if(Character.isdigit (temp)) {count= count * + (Temp-' 0 ');//consecutive numbersIsnum =true; }Else{ if(Isnum) {//encountering symbols pressing the front digitsNum.push (count); Count= 0; Isnum=false; if(!symbol.isempty () && (symbol.peek () = = ' * ' | | symbol.peek () = = '/')){ CharTempsymbol =Symbol.pop (); intb =Num.pop (); intA =Num.pop (); if(Tempsymbol = = ' * ') {Num.push (a*b); }Else{Num.push (a/b); } } } if(Temp! = ' # ') {//press the symbol intoSymbol.push (temp); } } } if(!Symbol.isempty ()) { intRep = 0; while(!symbol.isempty ()) {//Add and subtract operations CharTempsymbol =Symbol.pop (); intA =Num.pop (); if(Tempsymbol = = ' + ')) {Rep+=A; }Else{Rep-=A; }} Num.push (Rep+Num.pop ()); } returnNum.pop (); }}
[Leetcode-java] Basic Calculator II