在不使用额外的内存空间的条件下判断一个整数是否是回文。
回文指逆序和正序完全相同。
数据范围:
进阶: 空间复杂度
,时间复杂度
提示:
负整数可以是回文吗?(比如-1)
如果你在考虑将数字转化为字符串的话,请注意一下不能使用额外空间的限制
你可以将整数翻转。但是,如果你做过题目“反转数字”,你会知道将整数翻转可能会出现溢出的情况,你怎么处理这个问题?
function isPalindrome( x ) {
// write code here
if(x>=0){ /// >=0的时候做整数反转 判断翻转后的数字是否和原数字相等
let res = 0,a = x
while(a!==0){
res = res*10 + a% 10
a = (a/10) |0
}
(res|0) === res? res : 0
return res == x ? true : false
}
return false //小于0 肯定不是回文数字
} /**
*
* @param x int整型
* @return bool布尔型
*/
function isPalindrome( x ) {
// write code here
if(x<0 || x!==0 && x%10===0)return false;
// 当x是个位数时一定是回文
if(x<=9)return true
var halfReverse = 0;
while(x > halfReverse){
halfReverse = halfReverse*10+x%10;
x = parseInt(x/10);
}
return (halfReverse===x||halfReverse/10 === x)
}
module.exports = {
isPalindrome : isPalindrome
};