首页 > 其他 > 详细

[LeetCode] Valid Palindrome

时间:2015-03-03 01:04:11      阅读:216      评论:0      收藏:0      [点我收藏+]

代码:

class Solution {
public:
    bool isValid(string s) {
        string left = "([{";
        string right = ")]}";
        stack<char> stk;

        for(auto c : s) {
            if(left.find(c) != string::npos)
                stk.push(c);
            else {
                if(stk.empty() || stk.top() != left[right.find(c)])
                    return false;
                else
                    stk.pop();
            }
        }

        return stk.empty();
    }
};

 

杂记:

1. string::npos

2. 其他解法一

不使用占,递归替换符号

[LeetCode] Valid Palindrome

原文:http://www.cnblogs.com/Azurewing/p/4310032.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!