码迷,mamicode.com
首页 > 其他好文 > 详细

Valid Parentheses

时间:2017-04-26 10:13:37      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:als   char   amp   style   else   str   push   pop   处理   

方法:直接使用栈的数据结构对相关字符处理即可

class Solution {
public:
    bool isValid(string s) {
        stack<char> st;
    
        for(int i=0; i<s.size(); ++i)
        {
            if(s[i] == ‘(‘ || s[i] == ‘{‘ || s[i] == ‘[‘)
                st.push(s[i]);
            else
            {
                if(st.empty())
                    return false;
                if(s[i] == ‘)‘ && st.top() != ‘(‘)
                    return false;
                if(s[i] == ‘}‘ && st.top() != ‘{‘)
                    return false;
                if(s[i] == ‘]‘ && st.top() != ‘[‘)
                    return false;
 
                st.pop();
            }
        }
        
        return st.empty();
    }
};

 

Valid Parentheses

标签:als   char   amp   style   else   str   push   pop   处理   

原文地址:http://www.cnblogs.com/chengyuz/p/6766832.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!