# My java solution without Stack

• but uses too much time...

anyone can improve this code?

public static int calculate(String s) {

``````	s = s.replace(" ", "");

if(s=="") return 0;
else{
if(s.contains("(")){
int a = 0;
int b = 0;
int flag = 0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='('){
flag++;
}
if(flag==1&&s.charAt(i)=='(') a=i;
if(s.charAt(i)==')'){
flag--;
}
if(flag==0&&s.charAt(i)==')') b=i;
}
String s1 = s.substring(0,a);
String s2 = s.substring(a+1, b);
String s3 = s.substring(b+1);
s2 = String.valueOf(calculate(s2));
s = s1+s2+s3;
s = s.replace("++", "+");
s = s.replace("+-", "-");
s = s.replace("-+", "-");
s = s.replace("--", "+");
}

//calculate easy style;
int c=0;
for(int i=s.length()-1;i>=0;i--){
if((s.charAt(i)=='+'||s.charAt(i)=='-')&&i!=0){
c=i;
break;
}
}
if(c==0) return Integer.parseInt(s);
else if(s.charAt(c)=='+') return calculate(s.substring(0, c))+Integer.parseInt(s.substring(c+1));
else return calculate(s.substring(0, c))-Integer.parseInt(s.substring(c+1));
}
}``````

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