Palindrome Number (Java Solutions)

  • 0

    You can solve this question using two different methods.

    Convert Integer to String
    Convert integer to String and set two pointers at start and end of the string.and check if these characters are same, if they are not then its not a palindrome ( just like you check whether a string is palindrome or not).

         public boolean isPalindrome(int x) {
            if(x<0){return false;}
            String a=Integer.toString(x);
            int i=0;int j=a.length()-1;
                if(a.charAt(i)!=a.charAt(j)){return false;}
            return true;

    Doing Manipulation on Integer itself
    Calculate the length of integer (For ex length of 123 is 3). and set two integers l and r where x/l is leftmost integer and x%10 is rightmost integer.If these integers are not same then integer is not a palindrome else check for remaining number which is x%l will remove leftmost integer and x/10 will remove rightmost.

         public boolean isPalindrome(int x) {
            if(x<0){return false;}
            int b=x;
            int z=-1;
            if(z==0){return true;}
            int l=(int)Math.pow(10,z);
            int r=10;
                    int a1=x/l;
                    int b1=x%10;
                    if(a1!=b1){return false;}
            return true;

Log in to reply

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