LeetCode:20、有效的括号 (C++ 两种解法)

LeetCode: 20、有效的括号
C++ 两种解法
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。

思路:遇到一个左括号就将它存到容器(或栈)中,遇到一个右括号就判断此时容器中顶部元素是不是对应的左括号,如果是,则这对括号是对应的,此时再将这个顶部元素对应的左括号去除。

第一种解法:deque容器

class Solution {
    
    
public:
    bool isValid(string s) {
    
    

        if(s.length()%2==0){
    
    
           // Stack<Character> stack=new Stack<>();
           
           deque<char> st;
            for(int i=0;i<s.length();i++){
    
    
                char c=s.at(i);
                if(c=='('||c=='['||c=='{'){
    
    
                    st.push_front(c);
                }
                else if(c==')'){
    
    
                    if(!st.empty()&&st.front()=='(')
                    {
    
    
                        st.pop_front();
                    }
                    else
                    {
    
    
                        return false;
                    }
                }
                else if(c=='}')
                {
    
    
                    if(!st.empty()&&st.front()=='{')
                    {
    
    
                        st.pop_front();
                    }
                    else
                    {
    
    
                        return false;
                    }
                }
                else{
    
    
                    if(!st.empty()&&st.front()=='[')
                    {
    
    
                        st.pop_front();
                    }
                    else
                    {
    
    
                        return false;
                    }
                }
            }
            return st.empty();
        }
        return false;
	}
};

第二种解法:stack栈容器解法

class Solution {
    
    
public:
    bool isValid(string s) {
    
    

        if(s.length()%2==0)
        {
    
    
           // Stack<Character> stack=new Stack<>();
           
           stack<char> st;
            for(int i=0;i<s.length();i++)
            {
    
    
                char c=s.at(i);
                if(c=='('||c=='['||c=='{')
                {
    
    
                    st.push(c);
                }
                else if(c==')'){
    
    
                    if(!st.empty()&&st.top()=='(')
                    {
    
    
                        st.pop();
                    }
                    else
                    {
    
    
                        return false;
                    }
                }
                else if(c=='}')
                {
    
    
                    if(!st.empty()&&st.top()=='{')
                    {
    
    
                        st.pop();
                    }
                    else
                    {
    
    
                        return false;
                    }
                }
                else{
    
    
                    if(!st.empty()&&st.top()=='[')
                    {
    
    
                        st.pop();
                    }
                    else
                    {
    
    
                        return false;
                    }
                }
            }
            return st.empty();
        }
        return false;

    }
};

猜你喜欢

转载自blog.csdn.net/yuzhongmanbu99/article/details/117379237
今日推荐