zoukankan      html  css  js  c++  java
  • 【leetcode】20. Valid Parentheses

    @requires_authorization
    @author johnsondu
    @create_time 2015.7.13 11:03
    @url [valid parentheses](https://leetcode.com/problems/valid-parentheses/)
    /*****************
     * 类别: 栈模拟推断
     * 时间复杂度: O(n)
     * 空间复杂度: O(n)
     ****************/
    class Solution {
    public:
        bool isValid(string s) {
            stack<char> st;
            int len = s.size();
            for(int i = 0; i < len; i ++){
                if(s[i] == ']' || s[i] == '}' || s[i] == ')'){
                    if(st.empty()){
                        return false;
                    }
                }
                else{
                    st.push(s[i]);
                    continue;
                }
                if(s[i] == ']'){
                    char ch = st.top();
                    st.pop();
                    if(ch != '[') return false;
                    continue;
                }
                if(s[i] == ')'){
                    char ch = st.top();
                    st.pop();
                    if(ch != '(') return false;
                    continue;
                }
                if(s[i] == '}'){
                    char ch = st.top();
                    st.pop();
                    if(ch != '{') return false;
                    continue;
                }
            }
            if(st.empty()) return true;
            else return false;
        }
    };
  • 相关阅读:
    7.Mongodb安全性流程
    6.Mongodb索引
    5.Mongodb聚合
    8-进程管理
    7-安装包管理
    6-文件系统
    5-权限用户组
    27-ATM+购物车程序
    26.本章小结
    名词解释
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7219243.html
Copyright © 2011-2022 走看看