判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
python:将整数转换成字符
class Solution:
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
x=str(x)
x_len=len(x)
for i in range((int(x_len/2)+1)):
if x[i]!=x[x_len-1-i]:
return False
break;
return True
进阶:不使用字符串
class Solution:
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
x1=x
new_x=0
while(x1>0):
value=x1%10
x1=int(x1/10)
new_x=new_x*10+value
if x!=new_x:
return False
else:
return True