zoukankan      html  css  js  c++  java
  • [LeetCode20]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.

    代码:

    class Solution {
    public:
        bool isValid(string s) {
            stack<char> vec;
            for(char c : s)
            {
                switch(c)
                {
                case '(':
                case '[':
                case '{':
                    vec.push(c);
                    break;
                case ')':
                    if(vec.empty() || vec.top() != '(')
                        return false;
                    else
                        vec.pop();
                    break;
                case ']':
                    if(vec.empty() || vec.top() != '[')
                        return false;
                    else
                        vec.pop();
                    break;
                case '}':
                    if(vec.empty() || vec.top() != '{')
                        return false;
                    else
                        vec.pop();
                    break;
                }
            }
            return vec.empty();
        }
    };
  • 相关阅读:
    功能测试
    数据库
    loadrunner
    笔记
    基础知识
    类方法, 实例方法, 静态方法
    统计英文单词次数
    合并文件内容
    字典排序
    排序算法
  • 原文地址:https://www.cnblogs.com/zhangbaochong/p/5250284.html
Copyright © 2011-2022 走看看