09.回文数
判断一个整数是否回文数。当一个整数正序或倒序读都是一样的,那么它是回文数。
####例子1:
Input: 121
Output: true
####例子2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
####例子3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
####进阶: 不将整数转化为字符串解决这个问题。
####解法: 和翻转整数一样的思路,翻转后对比翻转前后是否相等。
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
let init = x;
let res = 0;
while (x > 0) {
res = res * 10 + x % 10;
x = Math.floor(x / 10);
}
return init === res;
};