Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

def isPalindrome(x: Int): Boolean = {
  if (x < 0 || (x != 0 && x % 10 == 0)) false
  else {
    var _x = x
    var res = 0
    while (x > res) {
      res = res * 10 + _x % 10
      _x /= 10
    }
    x == res || x == res / 10
  }
}

Last updated