Simple java solution with recursion.


  • 7
    Z
        import java.util.*;
    
    public class Solution {
        public int calculate(String s) {
            return calc(new StringTokenizer(s, " ()+-", true));
        }
        
        int calc(StringTokenizer st){
            int sofar = 0;
            boolean plus = true; // last seen operator.
            while(st.hasMoreTokens()){
                int val =0;
                String next = st.nextToken();
                switch(next){
                    case "(":
                        val = calc(st);
                        sofar += (plus ?  val : -val);
                        break;
                    case ")":
                        return sofar;
                    case "+":
                        plus = true;
                        break;
                    case "-":
                        plus = false;
                        break;
                    case " ": // no-op
                        break;
                    default:
                        val = Integer.parseInt(next);
                        sofar += (plus ?  val : -val);
                        break;
                }
                
            }
            
            return sofar;
        }
    }
    

    Explained:
    calc(), the working horse, is using a simple strategy: if "(" spotted, dive one level deeper in recursion. when it sees matching ")", return with an integer value of expression between "(...)" . Each call of calc() handles expression within one pair of "(...)".

    Within one pair of "(..)", generally there are only two cases: operator and operand. It's easy to handle them respectively. Slightly tricky thing is remembering last seen operator which is + or -.


  • 0
    V

    excellent solution!
    By using StringTokenizer can save many small mistakes.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.