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

LeetCode:Valid Parentheses

时间:2016-06-05 12:37:51      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

Valid Parentheses


Total Accepted: 111710 Total Submissions: 375574 Difficulty: Easy

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.

Subscribe to see which companies asked this question

Hide Tags
 Stack String














c++ code:

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


LeetCode:Valid Parentheses

标签:

原文地址:http://blog.csdn.net/itismelzp/article/details/51588430

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