[leetcode 125] Valid Palindrome

来源:互联网 发布:淘宝捡漏qq群 编辑:程序博客网 时间:2024/05/24 01:44

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) {        const int n = s.size();        if (n==0) {            return true;        }        int l = 0;        int r = n - 1;        while (l <= r) {            while (l < r && isalnum(s[l]) == false) {                l++;            }            while (r > l && isalnum(s[r]) == false) {                r--;            }            if (toupper(s[l]) != toupper(s[r])) {                return false;            }            l++;            r--;        }        return true;    }};


0 0
原创粉丝点击