/**
* @param {number} x
* @returns {boolean}
* 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
*/
function isPalindrome(x) {
while(typeof x !== 'number' || x < 0){
return false
}
console.log(reverse(x));
return x == reverse(x) ? true : false
}
// const num = 12345
const num1 = 12321
const result = isPalindrome(num1)
console.log(result);
function reverse(x) {
let num = x > 0? x.toString().split(''): (-x).toString().split('')
let target = x> 0? true: false
const length = num.length
if(typeof x === "number" && length >0 ){
const index = Math.floor(length/2)
for(let i =0; i<=index; i++) {
temp = num[i]
num[i] = num[length -i]
num[length -i] = temp
}
target? '': num[0] = '-'
return num.join('')
}
}
leetcode 9-回文数
猜你喜欢
转载自blog.csdn.net/weixin_40944062/article/details/113104976
今日推荐
周排行