题解 | #数字字符串转化成IP地址#

数字字符串转化成IP地址

https://www.nowcoder.com/practice/ce73540d47374dbe85b3125f57727e1e

class Solution:
    def restoreIpAddresses(self , s: str) -> List[str]:
        if len(s) < 4 or len(s) > 12:
            return None

        res = []
        path = []
        def backrack(idx):
            if len(path) == 4 and idx == len(s):
                res.append('.'.join(path))
                return
            
            for i in range(1, 4):
                if idx+i > len(s): 
                    continue
                sub = s[idx:idx+i]
                if len(sub) > 1 and sub[0] == '0':
                    continue
                if int(sub) > 255:
                    continue
                
                path.append(sub)
                backrack(idx+i)
                path.pop()

        backrack(0)
        return res

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务