1. 程式人生 > >劃分ip地址 python

劃分ip地址 python

class Solution2(object):

    def dfs(self, s, segment, res, tmp):
        if segment == 4 and s == "":
            res.append(tmp[1:])
            return
        for i in range(1, 4):
            if i <= len(s):
                if s[0] == '0' or int(s[:i]) > 256:
                    break
                if int(s[:i]) <= 255:
                    self.dfs(s[i:], segment + 1, res, tmp + '.' + s[:i])
        
    def RestoreIp(self, s):
        res = list()
        self.dfs(s, 0, res, '')
        return  res