class Solution {
public:
int scoreOfParentheses(string s) {
stack<string>stk;
int res=0;
for(int i=0;i<s.size();i++)
{
if(s[i]=='(')
stk.push("(");
else{
int temp=0;
while(stk.top()!="(")
{
temp+=stoi(stk.top());
stk.pop();
}
if(temp==0)
{
stk.pop();
stk.push("1");
}
else
{
stk.pop();
stk.push(to_string(temp*2));
}
//为什么上面的if-else可以但是下面的不行,即使再设置一个变量接收max也不行
//报错:terminate called after throwing an instance of 'std::invalid_argument' what(): stoi
// stk.push(to_string(max(1,temp*2)));
}
}
while(!stk.empty())
{
res+=stoi(stk.top());
stk.pop();
}
return res;
}
};