1. 程式人生 > >93. 復原IP地址

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)