题解 | #有效括号序列#

有效括号序列

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;
    }
};
全部评论

相关推荐

Beeee0927:是缅甸园区吗
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
今天 12:20
点赞 评论 收藏
分享
湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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