93. 复原IP地址

在这里插入图片描述

class Solution:
    def restoreIpAddresses(self, s):
        """
        :type s: str
        :rtype: List[str]
        """
        if len(s) > 12:
            return []
        res = []
        self.dfs(s, [], res)
        return res
    
    def dfs(self, s, path, res):
        if not s and len(path) == 4:
            res.append('.'.join(path))
        for i in range(1,4):
            if i > len(s):
                continue
            number = int(s[:i])
            if str(number) == s[:i] and number <= 255:
                self.dfs(s[i:], path + [s[:i]], res)

猜你喜欢

转载自blog.csdn.net/qq_38484259/article/details/84956389