Skip to content

9. Palindrome Number

https://leetcode.com/problems/palindrome-number/

js
/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
  if (x < 0) {
    return false
  }

  var copy = x
  var reverse = 0

  while (copy) {
    reverse = reverse * 10 + copy % 10
    copy = copy / 10 >> 0
  }

  return x === reverse
}
py
class Solution(object):

    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x < 0:
            return False
        copy, reverse = x, 0
        while copy:
            reverse = reverse * 10 + copy % 10
            copy = copy // 10
        return x == reverse
go
func isPalindrome(x int) bool {
	if x < 0 {
		return false
	}

	copyed, reversed := x, 0

	for copyed > 0 {
		reversed = reversed*10 + copyed%10
		copyed = copyed / 10
	}

	return x == reversed
}