Can anybody help me check why the result is output limit exceeded[python]


  • 0
    D

    I've tested on my local computer and couldn't find any wrong answer.

     class Solution:
            # @param s, a string
            # @return a list of strings
            
            def helper(self,s,temp,level,output):
                if level>3:
                    if not s:
                        output.append(".".join(temp))
                        return
                length = min(len(s),3)
                for i in range(length):
                    val = int(s[:i+1])
                    if s[0]=='0' and i==0 or 0 < val<=255:
                        temp.append(str(val))
                        self.helper(s[i+1:],temp,level+1,output)
                        temp.pop()
                    
            def restoreIpAddresses(self, s):
                output = []
                self.helper(s,[],0,output)
                return output

  • 0
    L

    The condition "s[0]=='0' and i==0 or 0 < val<=255" should be strengthened.

    what if s[:i+1]='010'? then val<=255 still holds. However it not an valid string for that integer.

    I used similar condition as you before and encountered false solution. May you try this instead:

    tmp=s[0:ii]                
    if len(tmp)==1 or (len(tmp)==2 and tmp[0]!='0') or (len(tmp)==3 and tmp[0]!='0' and int(tmp[0:3])<=255):
    

Log in to reply
 

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