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

[LeetCode] Valid Palindrome

时间:2015-07-14 09:48:40      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

Question:

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.

1、题型分类:

2、思路:采用双指针,一个从前一个从后。

3、时间复杂度:

4、代码:

    public boolean isPalindrome(String s) {
        if(s.length()<=1) return true;
        int begin=0,end=s.length()-1;
        while(begin<=end)
        {
            char b=s.charAt(begin);
            char e=s.charAt(end);
            if(!isCharacterNummeric(b))
            {
                begin++;
                continue;
            }
            if(!isCharacterNummeric(e))
            {
                end--;
                continue;
            }
            if(intoLowwerCase(b)!=intoLowwerCase(e))
                return false;
            else { begin++;end--;}
        }
        return true;
    }
    public boolean isCharacterNummeric(char c)
    {
        return (c>=‘a‘ && c<=‘z‘) || (c>=‘A‘ && c<=‘Z‘) || (c>=‘0‘ && c<=‘9‘);
    }
    public char intoLowwerCase(char c)
    {
        if(c>=‘a‘ && c<=‘z‘) return (char)(c-32);
        return c;
    }

 

5、优化:

6、扩展:

[LeetCode] Valid Palindrome

标签:

原文地址:http://www.cnblogs.com/maydow/p/4644575.html

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