1.利用字符串中的pop()方法【双向队列】
def isPalindrome(x: int):
lst = list(str(x))
while len(lst) > 1:
# 头尾删除,判断是否一致
# 一致继续判断,否则直接返回False
if lst.pop(0) != lst.pop():
return False
return True
2.利用字符串索引方式判断【双指针】
def isPalindrome(x: int) -> bool:
lst = list(str(x))
# L,R为字符串的左、右索引
L, R = 0, len(lst)-1
while L <= R:
if lst[L] != lst[R]:
return False
# 当lst[L]等于lst[R]时,左索引值+1,右索引值-1
L += 1
R -= 1
return True
给出一个列表,和一个目标数,找出和为目标值的两个数,返回其索引
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in range(len(nums)-1):
for n in range(len(nums)-1):
if n>=i:
if nums[i]+nums[n+1]!= target:
n = n + 1
else:
return [i,n+1]
if __name__=='__main__':
a = Solution()
result = a.twoSum([2,7,11,15],9)
print(result)