题解 | #有效括号序列#

有效括号序列

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

/*
	HW栈1 括号序列

*/


#include <bits/stdc++.h>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @return bool布尔型
     */
    static bool isbro(char x,char y)
    {
        if(min(x,y)=='('&& max(x,y)==')')return true;
        if(min(x,y)=='{'&& max(x,y)=='}')return true;
        if(min(x,y)=='['&& max(x,y)==']')return true;
        return false;
    }

    bool isValid(string s) {
        // write code here
        stack<char>st;
        if(s.length()==0||s.length()==1)return false;
        st.push(s[0]);
        for(int i=1;i<s.length();i++){
            if(st.empty()){
                st.push(s[i]);
                continue;
            }
            if(isbro(st.top(), s[i]))st.pop();
            else{
                st.push(s[i]);
            }
        }
        if(st.empty())return true;
        return false;
    }
};
全部评论

相关推荐

爱吃烤肠的牛油最喜欢...:50K是ssp了估计,ssp的人家多厉害都不用说,每年比例大概在百分之5左右
点赞 评论 收藏
分享
野猪不是猪🐗:还是太卑微了,什么叫放弃本次面试应该说经过评估,贵公司与自己不匹配,决定不再推进后续流程
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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