zoukankan      html  css  js  c++  java
  • [LeetCode] #20 Valid Parentheses

    Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

    The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

    本题是简单匹配问题,用stack最优。时间:2ms

    我的代码:

    class Solution {
    public:
        bool isValid(string s) {
            stack<char> sign;
            for (string::const_iterator iter = s.begin(); iter != s.end(); ++iter){
                switch (*iter){
                    case '(':
                    case '{':
                    case '[':sign.push(*iter); break;
                    case ')':
                        if (sign.size()&&sign.top() == '('){
                            sign.pop(); break;
                        }
                    case '}':
                        if (sign.size() && sign.top() == '{'){
                            sign.pop(); break;
                        }
                    case ']':
                        if (sign.size() && sign.top() == '['){
                            sign.pop(); break;
                        }
                        return false;
                    default:break;
                }
            }
            if (sign.size())
                return false;
            else
                return true;
        }
    };
    “If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.”
  • 相关阅读:
    [PHP] Layui + jquery 实现 实用的文章自定义标签
    个人总结第五周
    个人总结第四周
    个人总结第三周
    个人总结第二周
    个人总结第一周
    用户体验评价
    第二阶段scrum冲刺
    单词统计
    用户模块和用户场景
  • 原文地址:https://www.cnblogs.com/Scorpio989/p/4451713.html
Copyright © 2011-2022 走看看