My accepted solution in Java , which I think is really smart


  • 2
    A

    1.char[] ary=s.toCharArray();

    2.Start to travel ary

    • If given parameter a is ''Valid Palindrome''
    • Then for each element which is a alphanumeric character in ary
    • There must be a same element at a correct position from the end of the ary
    • So, If I couldn't find it , it would be fasle, or it will be true

    public class Solution {
        public boolean isPalindrome(String s) {
            if(s.length()==0 || s.length()==1) return true;
            int length=0;
            char[] ary=s.toCharArray();
            int j=ary.length-1;
            for(int i=0;i<ary.length;i++)
            {
                if(('a'<=ary[i] && ary[i]<='z')||('A'<=ary[i] && ary[i]<='Z')||('0'<=ary[i] && ary[i]<='9'))
                {
                    while(true)
                    {
                        if(j==-1) return false;
                        if(('a'<=ary[j] && ary[j]<='z')||('A'<=ary[j] && ary[j]<='Z')||('0'<=ary[j] && ary[j]<='9'))
                        {
                            if((ary[j]!=ary[i]) && (ary[j]+32!=ary[i]) && (ary[j]-32!=ary[i])) return false;
                            j--;
                            break;
                        }
                        j--;
                    }
                }
            }
            return true;
        }
    }

  • 16
    Z
    def isPalindrome(s):
        newS= [i.lower() for i in s if i.isalnum()]
        return newS == newS[::-1]
    

    life is short, you need python


  • 0
    A
    This post is deleted!

  • 0
    X

    you're so smart... i'm shocked


  • 0
    X

    Hi man, you are so cool


  • 0
    L

    me too. hahaha


  • 0
    L

    WTF.....dude your code is killing me.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.