做的这两道题,到现在还没通过,只好先放一下了。-_-||
5题代码:
1 class Solution(object): 2 def longestPalindrome(self, s): 3 """ 4 :type s: str 5 :rtype: str 6 """ 7 temp=[] 8 list1=list(s) 9 l=len(list1) 10 list2=list1[0:1] 11 l2=0 12 if l==0: 13 return "" 14 if l==1: 15 return s 16 else: 17 i=1 18 while i<2*l: 19 list1.insert(i,'@') 20 i+=2 21 l=len(list1) 22 i=1 23 while i<l: 24 j=0 25 while j<=i and i+j<l: 26 if list1[i-j]==list1[i+j]: 27 temp=list1[i-j:i+j+1] 28 j+=1 29 else: 30 break 31 if len(temp)>l2: 32 list2=temp 33 l2=len(list2) 34 i+=1 35 result="" 36 for i in list2: 37 if i!="@": 38 result+=i 39 return result
15题代码:
class Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ result=[] temp=[] nums.sort() i=0 l=len(nums) while i<l-2: left=i+1 right=l-1 if nums[i]>0 or nums[right]<0: break while left<right and nums[left]+nums[i]<=0 and nums[right]>=0: s=nums[i]+nums[left]+nums[right] if s==0 and [nums[i],nums[left],nums[right]] not in result: temp=[nums[i],nums[left],nums[right]] result.append(temp) left+=1 right-=1 elif s>0: right-=1 else: left+=1 i+=1 return result