python leetcode 93. Restore IP Addresses

Neekity發表於2018-12-10

又是DFS

class Solution(object):
    def restoreIpAddresses(self, s):
        """
        :type s: str
        :rtype: List[str]
        """
        self.res=set()
        def help(s1,s2,k):
            if s2=="" and k==0:
                return self.res.add(s1[:-1])
            if k<=0 or s2=="":
                return 
            for i in range(1,4):
                if 0<=int(s2[:i])<=255:
                    if len(s2[:i])>1 and s2[0]=='0':
                        break
                    help(s1+s2[:i]+'.',s2[i:],k-1)
        help("",s,4)
        return list(self.res)

相關文章