#include <queue>
#include <stack>
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param s string字符串
     * @return bool布尔型
     */
    stack<char>qu;
    bool is_valid_cow_communication(string s) {
        int ls = s.length();
        int cnt = 0;
        while (cnt <= ls) {
            if (s[cnt] == '(')
                qu.push('(');
            if (s[cnt] == '{')
                qu.push('{');
            if (s[cnt] == '[')
                qu.push('[');
            if (s[cnt] == ')') {
                if (qu.top() == '(')
                    qu.pop();
                else
                    return false;
            }
            if (s[cnt] == '}') {
                if (qu.top() == '{')
                    qu.pop();
                else
                    return false;
            }
            if (s[cnt] == ']') {
                if (qu.top() == '[')
                    qu.pop();
                else
                    return false;
            }
            ++cnt;
        }
        if (cnt < ls - 1)
            return false;
        else
            return true;
    }
};

一、题目考察的知识点

栈+模拟

二、题目解答方法的文字分析

就是遇到左边的括号就入栈,栈的特性是先进后出,遇到右边的括号,就判断一下如果栈顶刚好是相对应的左括号,那么就删除栈顶,如果不是就直接判断为负,然后依次遍历下去,最后判断一下栈里面还有没有元素,如果还有直接判负,并且判断一下栈外面还有没有元素。

三、本题解析所用的编程语言

c++