payal-kothari
7/15/2017 - 1:41 AM

From https://leetcode.com/problems/palindrome-number/#/description

public class Solution {
    public boolean isPalindrome(int x) {
        
        if (x< 0 || (x !=0 && x%10==0)){        // for -ve value, and if x!=0 but multiple of 10 (e.x 20,400etc.)
            return false;
        }
        
        int num = x/10;    
        if (num == 0){                          // for single digit number
            return true;
        }
        
        int half = 0;
        while(x > half){                        // remaining numbers
            half = half * 10 + x%10;
            x = x/10;
        }
        
        return( x == half || x == half/10);     // for even and odd length numbers
        
        
// ******** Answer using string, will need extra space        
        
        
//         String str = String.valueOf(x);
//         char[] charArray = str.toCharArray();
        
//         if (str.length() == 1){
//             return true;
//         }
        
//         int i = 0;
//         int j = str.length() -1;
        
//         while (i<j) {
//             if (charArray[i] == charArray[j]){
//                 i++;
//                 j--;
//             }
//             else{
//                 return false;
//             }
//         }
        
//         return true;
    }
}