题解 | 有效括号序列

有效括号序列

https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2

#include <cstring>
#include <stack>
class Solution {
  public:

    stack<char>  C_stack;
    bool isValid(string s) {
        for (int i = 0; i < s.length(); i++) {
            if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
                C_stack.push(s[i]);
            } else {
                 if(!C_stack.empty()){
                    char temp_char=C_stack.top();
                    C_stack.pop();
                    if(s[i]==')'){
                        if(temp_char!='(')
                          return false;
                    }

                    if(s[i]==']'){
                        if(temp_char!='[')
                          return false;
                    }

                    if(s[i]=='}'){
                        if(temp_char!='{')
                          return false;
                    }

                  }else {
                     return false;
                  }
                }
        }


        if(C_stack.empty()){
            return true;
        }else {
           return false;
        }
            }
} ;           

全部评论

相关推荐

迷茫的大四🐶:💐孝子启动失败,改为启动咏鹅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务