My submit Beat 98.65%


  • 0
    7

    public class Solution {
    int i=0;
    public int calculate(String s) {
    int re=0;
    int num=0;
    char op='+';
    while(i<=s.length()){
    if(i==s.length()){
    re=op(re,num,op);i++; break;
    }
    char ch=s.charAt(i);
    if(ch>='0'&&ch<='9'){
    num=num*10+ch-'0';
    }else if(ch=='+'||ch=='-'){
    re=op(re,num,op);
    num=0;op=ch;
    }else if(ch=='('){
    i++;
    re=op(re,calculate(s),op);
    }else if(ch==')'){
    re=op(re,num,op);
    return re;
    }
    i++;
    }
    return re;
    }
    public int op(int a,int b,char ch){
    if(ch=='+') return a+b;
    else return a-b;
    }

    }


Log in to reply
 

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