LeetCode-Valid Palindrome

来源:互联网 发布:水利造价软件 编辑:程序博客网 时间:2024/04/29 05:49

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.

Solution:

Code:

<span style="font-size:14px;">class Solution {public:    bool isPalindrome(string s) {        int begin = 0, end = s.size()-1;        while (begin < end) {            if ((isalpha(s[begin]) || isdigit(s[begin])) && (isalpha(s[end]) || isdigit(s[end]))) {                if (tolower(s[begin]) != tolower(s[end]))                    return false;                ++begin;                --end;                continue;            }            if (!isalpha(s[begin]) && !isdigit(s[begin]))                ++begin;            if (!isalpha(s[end]) && !isdigit(s[end]))                --end;        }        return true;    }};</span>



0 0
原创粉丝点击