leetcode 20:有效的括号

bool isValid(string s) {
    stack<int> sta;
    if(s.size()%2!=0){
        return false;
    }
    for(int i=0;i<s.size();i++){
        if(sta.empty()){
            sta.push(s[i]);
        }
        else if((sta.top()=='('&&s[i]==')')||(sta.top()=='['&&s[i]==']')||(sta.top()=='{'&&s[i]=='}')){
            sta.pop();
        }else
        {
            sta.push(s[i]);
        }

    }
    if(sta.empty()){
        return true;
    }
    return false;
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
  • 2021-08-08
  • 2021-05-29
猜你喜欢
  • 2021-05-29
  • 2021-09-25
  • 2021-11-22
  • 2021-07-03
  • 2021-06-24
  • 2021-07-07
  • 2021-07-03
相关资源
相似解决方案