Valid Palindrome


Question

http://www.lintcode.com/en/problem/valid-palindrome/

Discussion


比较简单。要跳过非字母和数字的字符,用isalnum函数。比较的时候要都转成小写或者大写再比较。

Solution


class Solution {
public:
    /**
     * @param s A string
     * @return Whether the string is a valid palindrome
     */
    bool isPalindrome(string& s) {
        // Write your code here
        if (s.length()==0)
            return true;
        int begin = 0;
        int end = s.length()-1;
        while(begin< end) {
            if(isalnum(s[begin]) == false)
                begin++;
            if(isalnum(s[end]) == false) 
                end--;
            if(begin>=end)
                break;
            if(tolower(s[begin]) != tolower(s[end]) ) 
                return false;
            begin++;
            end--;
        }
        return true;
    }
};

results matching ""

    No results matching ""