Palindrome Number


@baichuan0898 It's explained in the code:
// When the length is an odd number, we can get rid of the middle digit by revertedNumber/10 // For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123, // since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it. return x == revertedNumber  x == revertedNumber/10;

@kamfung2017 it's best if there's no integer overflow during the algorithm calculation. In many languages overflow behaviors are not defined. Although unlikely, who knows if it happens that some nonpalindrome number, after the overflow during the 'reverse' process, happen to equal to the original number :)?




@kaashmonee Great one line solution. However, if I'm not mistaken doesn't creating the str(x) and then recreating the inverse of using [::1] use O(n) space, instead of O(1)?


Accepted JAVA code using mathematical operations:
class Solution {
public boolean isPalindrome(int x) {
//handle negative integers
if(x<0)
return false;
//get the divisor for x
int div = 1;
while(x/div >= 10)
div = div*10;//check first and last digits if they are same while(x > 0){ if(x%10 != x/div) return false; x = (x%div)/10; div = div/100; } return true; }
}
Time complexity: O(logn)
Space complexity: O(1)