Why my python code TLE?


  • 0
    C
    class Solution:
        # @return an integer
        def lengthOfLongestSubstring(self, s):
            if len(s)==0:
                return 0
            dict=[-1]*256
            dict[ord(s[0])]=0
            max=1
            i=0
            j=1
            while i<len(s) and j<len(s):
                if dict[ord(s[j])]>0:
                    if j-i>max:
                        max=j-i
                    i=dict[ord(s[j])]+1
                    j=i+1
                    dict=[-1]*256
                    dict[ord(s[i])]=i
                else:
                    dict[ord(s[j])]=j
                    if max<j-i+1:
                        max=j-i+1
                    j+=1
            return max

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.