9.
class Solution(object):
def isPalindrome(self,x):
str1 = str(x) #数字转为字符串
length = len(str1)
tepstr = str1[::-1] #反转字符串
if(length==1):
return True
else:
for i,val_i in enumerate(str1):
if(i>(length/2-1)):
return True
if(val_i != tepstr[i]):
return False
return True
备注:
1.将一个数字转化为列表,用map;
>>> num=1234
>>> map(int,str(num))
[1, 2, 3, 4]
2.用str [:: - 1]来反转字符串;
3. 在网上找的另一种解答:
1 2 3 4 5 |
|
680.快排思想
class Solution(object):
def validPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
def myValidPalindrome(s, left, right):
while left < right:
if s[left] != s[right]:
return False
left, right = left + 1, right - 1
return True
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
return myValidPalindrome(s, left, right - 1) or myValidPalindrome(s, left + 1, right)
left, right = left + 1, right - 1
return True
647.动态规划 https://blog.csdn.net/OneDeveloper/article/details/79853156