34.leetcode15&5_time_limit_exceeded

做的这两道题,到现在还没通过,只好先放一下了。-_-||

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      

 

转载于:https://www.cnblogs.com/19991201xiao/p/8463933.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值