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

数字字符串转化成IP地址

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

class Solution:
    def restoreIpAddresses(self , s: str) -> List[str]:
        n = len(s)
        ans = []
        if not s:
            return ans
        def diedai(t,i,k):
            if i >= n:
                ans.append(t)
            if k == 3:
                if int(s[i]) > 0 and int(s[i:]) <256:
                    diedai(t+s[i:],n,k+1)
                elif int(s[i]) == 0 and i == n-1:
                    diedai(t+s[i:],n,k+1)
            if k < 4:
                if int(s[i]) >= 0 and i < n-1:
                        diedai(t+s[i]+'.',i+1,k+1)
                if int(s[i]) > 0 and i < n-2:
                    diedai(t+s[i:i+2]+'.',i+2,k+1)
                if 100 <= int(s[i:i+3]) <256 and i < n-3:
                    diedai(t+s[i:i+3]+'.',i+3,k+1)
        diedai('',0,0)
        return ans

全部评论

相关推荐

09-15 10:40
已编辑
门头沟学院 C++
牛客114_514_1919810号:离谱一律视为没hc假岗位,绝不把问题留给自己
投递哔哩哔哩等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务