zoukankan      html  css  js  c++  java
  • 20. 有效的括号(c++)

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
    有效字符串需满足:左括号必须用相同类型的右括号闭合。左括号必须以正确的顺序闭合。
    注意空字符串可被认为是有效字符串。
     
    class Solution {
    public:
        bool isValid(string s) {
            stack<char> m_stack;
            for(const auto&  v:s)
            {
                if(m_stack.empty())
                    m_stack.push(v);
                else if(compare(m_stack.top(),v))
                    m_stack.pop();
                else
                    m_stack.push(v);
            }
            return m_stack.size() == 0 ? true :false;
        }
    private:
        bool compare(const char& c1,const char& c2)
        {
            return (c1 == '[' && c2 == ']') || (c1 == '{' &&c2 == '}') || (c1 == '(' && c2 == ')');
        }
    };
  • 相关阅读:
    bzoj2818
    bzoj1901
    bzoj1010
    loj6277
    bzoj1001
    bzoj1787
    选项卡
    日期选择器
    去掉文本框的外边框
    bootstarp 模态框大小尺寸的控制
  • 原文地址:https://www.cnblogs.com/one-think/p/12593337.html
Copyright © 2011-2022 走看看