[LeetCode] Valid Palindrome

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)