LC 9. Palindrome Number

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
  if (x < 0 || (!(x % 10) && x)) return false
  let x2 = x, res = 0
  while(x2) {
    res = res * 10 + x2 % 10
    x2 = ~~(x2 / 10)
  }
  return res === x
};

~~ 在数字范围为 -2147483649 < value < 2147483648 时,相当于 Math.trunc()






评论和交流请发送邮件到 me@tianhegao.com





通过以下渠道赞赏此文