您的位置:首页 > 编程语言 > C语言/C++

[leetcode-125]Valid Palindrome(c++)

2015-08-12 16:07 393 查看
问题描述:

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,

“A man, a plan, a canal: Panama” is a palindrome.

“race a car” is not a palindrome.

Note:

Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

分析:注意过滤就好了。

代码如下:12ms

[code]class Solution {
public:
    char getValid(char s){
        if(s>='A'&&s<='Z')
            return s+32;
        else if(s>='a'&&s<='z')
            return s;
        else if(s>='0'&&s<='9')
            return s;
        else
            return 0;
    }
    bool isPalindrome(string s) {
        int length = s.size();
        int start = 0;
        int end = length-1;

        while(start<end){
            char sa = s[start];
            char ea = s[end];

            char ss = getValid(sa);
            if(!ss){
                start++;
                continue;
            }
            char ee = getValid(ea);
            if(!ee){
                end--;
                continue;
            }
            if(ss==ee){
                start++;
                end--;
            }else{
                return false;
            }
        }
        return true;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: