题解 | #有效括号序列#

有效括号序列

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

class Solution {
public:
    /**
     * 
     * @param s string字符串 
     * @return bool布尔型
     */
    bool isValid(string s) {
        // write code here
        if(s.length()%2!=0) return false;
        stack<char> st;
        for(int i=0;i<s.length();i++){
            if(st.size()==0){
                st.push(s[i]);
            }else{
                if(st.top()=='('&&s[i]==')') st.pop();
                else if(st.top()=='['&&s[i]==']') st.pop();
                else if(st.top()=='{'&&s[i]=='}') st.pop();
                else st.push(s[i]);
            }
        }
        return st.size()==0;
    }
};
全部评论

相关推荐

秋盈丶:后续:我在宿舍群里和大学同学分享了这事儿,我好兄弟气不过把他挂到某脉上了,10w+阅读量几百条评论,直接干成精品贴子,爽
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务