zoukankan      html  css  js  c++  java
  • Leetcode_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) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            stack<char> myStack;
            for(int i = 0; i< s.length(); i++)
            {
                switch(s[i]){
                    case '{': 
                    case '[':
                    case '(':  myStack.push(s[i]); break;
                    case ')':{
                            if(myStack.empty() ||myStack.top() != '(' )
                               return false;
                             myStack.pop();
                             break;
                    }
                    case ']': {
                            if(myStack.empty() ||myStack.top() != '[' )
                               return false;
                             myStack.pop();
                             break;
                    }
                    case '}': {
                            if(myStack.empty() ||myStack.top() != '{' )
                               return false;
                             myStack.pop();
                             break;
                    }
                    default : return false;
                }
            }
            if(myStack.empty())
                return true;
            else
                 return false;
        }
    };
    --------------------------------------------------------------------天道酬勤!
  • 相关阅读:
    BZOJ-3495 前缀优化建图2-SAT
    洛谷P3979 遥远的国度 树链剖分+分类讨论
    hdu
    hdu
    poj
    poj-1330(暴力写的lca)
    树链剖分
    Dijkstra
    Floyed
    最短路径
  • 原文地址:https://www.cnblogs.com/graph/p/3118382.html
Copyright © 2011-2022 走看看