leetcode-20-valid parentheses

class Solution {
public:
    bool isValid(string s) {
        if(s.empty())
            return true;
        stack<char> result;
        for(auto x:s){
            if(x == '(' || x == '[' || x == '{')
                result.push(x);
            else if(result.empty())
                return false;
            else if(x == ')'){
                if(result.top() == '(')
                    result.pop();
                else
                    return false;
            }
            else if(x == ']'){
                if(result.top() == '[')
                    result.pop();
                else
                    return false;
            }
            else if(x == '}'){
                if(result.top() == '{')
                    result.pop();
                else
                    return false;
            }
        }
        return result.empty();
    }
};

阅读更多
个人分类: leetcode
上一篇leetcode-14-longest common prefix
下一篇leetcode-21-merge two sorted lists
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭