Analyzing Python digital palindromic (string achieve transfer, does not turn the string implementation)

Judgment Python digital palindrome, the palindrome returns True, non-palindromic returns False, only a single-digit returns True.

In which a train of thought, convert a number to a string, and then contrast with the reverse, but requires extra space overhead to create a string. Implementation:

def isPalindrome(x):
"""
:type x: int
:rtype: bool
"""
str_x = str(x)
if len(str_x) == 0:
print("Input {0} is invalid.".format(x))
return False
return str_x == str_x[::-1]


if __name__ == '__main__':
input_str = input("Please input one number:")
try:
x = int(input_str)
print(isPalindrome(x))
except ValueError:
print("Input {0} is invalid.".format(input_str))


Guess you like

Origin www.cnblogs.com/kaiying-Tang/p/11511192.html