Public classSolution { Public intCalculate (String s) {Stack<String> stack =NewStack<string>(); S= S.replace ("", "" "); intLength =s.length (); intindex = 0; if(S.charat (0) <= ' 9 ' && s.charat (0) >= ' 0 ') Stack.push ("+"); while(Index <length) { CharCH =S.charat (index); if(Ch > ' 9 ' | | ch < ' 0 ') { if(ch = = ' + ' | | ch = = '-')) {Stack.push (string.valueof (ch)); Index++; } Else { intFirst =integer.valueof (Stack.pop ()); intTMP =index; Index= Helper (s, index + 1); intSecond = integer.valueof (s.substring (tmp + 1, index)); if(ch = = ' * ') {Stack.push (string.valueof ( first*second)); } Else{Stack.push (string.valueof ( first/second)); } } } Else { intTMP =index; Index= Helper (s, index + 1); Stack.push (s.substring (TMP, index)); } } intresult = 0; while(!Stack.isempty ()) { intTMP =integer.valueof (Stack.pop ()); intPositive = Stack.pop (). Equals ("+")? 1:-1; Result+ = tmp *positive; } returnresult; } Public intHelper (String s,intindex) { while(Index < S.length () && S.charat (index) <= ' 9 ' && S.charat (index) >= ' 0 ') {Index++; } returnindex; }}
[Leetcode] Basic Calculator II