数字字符串转IP地址

#数字字符串转IP地址
class Solution:
    def restoreIpAddresses(self, s):
        ans = []
        def judge_ip(s):
            if s[0]=='0':
                if len(s)==1:
                    return True
                else:
                    return False
            return int(s)<=255

        def dfs(s, n, path):
            if len(s)==0:
                return
            if n == 0:
                if judge_ip(s):
                    ans.append(".".join(path + [s]))
            else:
                for i in range(1, min(4,len(s))):
                    temp = s[:i]
                    if judge_ip(temp):
                        dfs(s[i:], n - 1, path + [temp])
        dfs(s, 3, [])
        return ans