You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

'''
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
示例 1输入: 121 输出: true
示例 2输入: -121 输出: false 解释: 从右向左读为 121- 。
示例 3输入: 10 输出: false 解释: 从右向左读为 01 。
'''
class Solution(object):
def _isPalindrome(self, x): # 列表法,用两个指针,一个从前,一个从后判断是否相同
"""
:type x: int
:rtype: bool
"""
str_x = list(str(x))
i , j = 0 , len(str_x) - 1
while i < j :
if str_x[i] != str_x[j]:
return False
i += 1
j -=1
return True
def isPalindrome(self, x: int): # 字符串法
if x < 0:
return False
t=str(x)
t =t[::-1] # 相当于t[-1:-len(t)-1:-1],从最后一个元素到第一个元素复制一遍,即倒序
t=int(t)
if x==t:
return True
else :
return False