<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p>

<p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;1 + 1&quot;
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot; 2-1 + 2 &quot;
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;(1+(4+5+2)-3)+(6+8)&quot;
<strong>Output:</strong> 23
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
	<li><code>s</code> consists of digits, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, and <code>&#39; &#39;</code>.</li>
	<li><code>s</code> represents a valid expression.</li>
	<li><code>&#39;+&#39;</code> is <strong>not</strong> used as a unary operation (i.e., <code>&quot;+1&quot;</code> and <code>&quot;+(2 + 3)&quot;</code> is invalid).</li>
	<li><code>&#39;-&#39;</code> could be used as a unary operation (i.e., <code>&quot;-1&quot;</code> and <code>&quot;-(2 + 3)&quot;</code> is valid).</li>
	<li>There will be no two consecutive operators in the input.</li>
	<li>Every number and running calculation will fit in a signed 32-bit integer.</li>
</ul>

class Solution {
public:
    int calculate(string s) {
        vector<string>v;
        // if(s[0]=='+'||s[0]=='-')s="0"+s;
      int i=0;
      while(i<s.size()){
        if(s[i]=='('||s[i]==')'||s[i]=='+'||s[i]=='-'){
            string temp;
            temp.push_back(s[i]);
            // cout<<temp<<endl;
            if(temp==")"&&v[v.size()-2]=="(")
              {
                v[v.size()-2]=v.back();
                 v.pop_back();
                }
            else if(temp=="-"){
                if(v.size()==0||v.back()=="(")v.push_back("0");
                 v.push_back(temp);

            }
            else 
            v.push_back(temp);

        }
        else if(s[i]>='0'&&s[i]<='9'){
            int j=i;
            while(j<s.size()&&s[j]>='0'&&s[j]<='9')j++;
            if(v.size()>1&&v[v.size()-2]=="("){
               string str=v.back();
                v.pop_back();
                if(str=="+")v.push_back(s.substr(i,j-i));
                else v.push_back("-"+s.substr(i,j-i));
            }
          else  v.push_back(s.substr(i,j-i));
            i=j-1;


        }
        i++;
      }
        stack<int>operand;
        stack<string>oper;
        // for(auto i:v)cout<<i<<" ";
        // cout<<"done"<<endl;
        
        for(auto i:v){
            // cout<<i<<" ";
            // if(operand.size())cout<<"top is "<<operand.top();
            // if(oper.size())cout<<"top oper is"<<oper.top();
            // cout<<endl;
            if((i[0]>='0'&&i[0]<='9')||i.size()>1)operand.push(stoi(i));
            else if(i=="(")oper.push(i);
            else if(i==")") {
                while(oper.top()!="("){
                    string op=oper.top();
                    // if(op=="(")continue;

                    oper.pop();
                    // if(oper)
                    int a=operand.top();
                    operand.pop();
                    int b=operand.top();
                    operand.pop();
                    if(op=="+")operand.push(a+b);
                    else operand.push(b-a);


                }
                oper.pop();
                // if(oper.size()&&oper.top()=="-"){
                //     int t=operand.top();
                //     operand.pop();
                //     operand.push(-t);
                //     oper.pop();
                //     oper.push("+");

                // }

            }
            else if(i=="+"||i=="-"){
                   while(oper.size()&&oper.top()!="("){
                    string op=oper.top();
                    // if(op=="(")continue;
                    oper.pop();
                    int a=operand.top();
                    operand.pop();
                    int b=operand.top();
                    operand.pop();
                    if(op=="+")operand.push(a+b);
                    else operand.push(b-a);


                }
                // if(oper.size()&&oper.top()=="("){

                // }
                oper.push(i);

            }
        }
        // cout<<"working"<<endl;
        // cout<<operand.size()<<oper.size()<<endl;
        while(operand.size()!=1){
              string  op=oper.top();
                    oper.pop();
                    int a=operand.top();
                    operand.pop();
                    int b=operand.top();
                    operand.pop();
                    // cout<<a<<" "<<op<<" "<<b<<endl;
                    if(op=="+")operand.push(a+b);
                    else operand.push(b-a);
        }
        // return -operand.top();
        return operand.top();
        
    }
};
32806