ALGORITHM

Renzo Regio
1 min readMay 6, 2021

--

TIL 2021.05.04

Solved in: Javascript and Python

9. Palindrome Number

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.

Example 1:

Input: x = 121
Output: true

Example 2:
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Example 4:
Input: x = -101
Output: false

Constraints:

  • -231 <= x <= 231–1

Solution: JavaScript

/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
let reversedNum = x.toString().split("").reverse().join("")
if(+reversedNum === x){
return true
} else {
return false
}
};

Solution: Python

class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
value = str(x)
num_list = []
for num in value:
num_list.append(num)
num_list.reverse()
num_str = ''.join(num_list)
if num_str == value:
return True
else:
return False

--

--