zoukankan      html  css  js  c++  java
  • 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      
  • 相关阅读:
    android有进度条的下载图片并且显示图片
    在Java中,直接将类的对象使用system.out.println输出
    改写toString
    Android中Uri的使用
    重写toString()
    权限管理
    实训
    第一次上传文件成功
    sql server 2005 JDBC连接遇到的问题
    JSP 权限控制
  • 原文地址:https://www.cnblogs.com/19991201xiao/p/8463933.html
Copyright © 2011-2022 走看看