LeetCode OJ Valid Palindrome

来源:互联网 发布:win7下怎么安装ubuntu 编辑:程序博客网 时间:2024/06/05 18:28

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.

直接在原字符串上进行判断,是数字字母就比较,不是就找到是为止。

class Solution {public:    bool isPalindrome(string s) {        for (int i = 0, j = s.size() - 1; i < j; i++, j--) {            while (i < s.size() && !isAlphanumeric(s[i])) i++;            while (j >= 0 && !isAlphanumeric(s[j])) j--;            if (i >= j) break;            if (!(s[i] == s[j] || s[i] - s[j] == 32 || s[j] - s[i] == 32)) return false;        }        return true;    }    inline bool isAlphanumeric(char a) {return (('a' <= a && a <= 'z') || ('A' <= a && a <= 'Z') || '0' <= a && a <= '9');} };


0 0
原创粉丝点击