码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode:Valid Palindrome

时间:2014-11-05 21:30:46      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:algorithm   leetcode   

题目描述:

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.

代码:

bool Solution::isPalindrome(string s)
{
    int head = 0;
    int tail = s.size()-1;
    while(head < tail)
    {
        while(!(isalpha(s[head]) || isalnum(s[head])) && head <= tail)
            head++;
        while(!(isalpha(s[tail]) || isalnum(s[tail])) && head <= tail)
            tail--;
        if(head < tail)
        {
            if((isalnum(s[head]) && isalnum(s[tail]) && s[head] == s[tail]) || isalpha(s[head]) && isalpha(s[tail]) && (s[head] == s[tail] || abs(s[head] - s[tail]) == 32))
            {
                head++;
                tail--;
            }
            else
                return false;
        }
    }
    return true;
}


LeetCode:Valid Palindrome

标签:algorithm   leetcode   

原文地址:http://blog.csdn.net/yao_wust/article/details/40832147

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!