Loading

20. 有效的括号

题目

 

代码

class Solution {
public:
    bool isValid(string s) {
       stack<char> sta;
       for(auto i:s)
       {
           switch(i)
           {
               case '[':
               case '(':
               case '{':sta.push(i);break;
                   
               case ']':if(sta.empty()||sta.top()!='[')return false;
                   else sta.pop();break;
               case ')':if(sta.empty()||sta.top()!='(')return false;
                   else sta.pop();break;  
               case '}':if(sta.empty()||sta.top()!='{')return false;
                   else sta.pop();break;
           }
       }
        return sta.empty();
    }
};

 

思路

利用一个辅助栈来存储 [、(、{ 

posted @ 2018-09-17 18:47  李正浩  阅读(69)  评论(0编辑  收藏  举报