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

数字字符串转化成IP地址

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

class Solution:
    def restoreIpAddresses(self , s: str) -> List[str]:
        # write code here
        n = len(s)
        ip_list =[]
        if n<4 or n >12:
            return []
        for i in range(1,n):
            for j in range(i+1,n):
                for k in range(j+1,n):
                    str1 = list(s)
                    str1.insert(k,'.')
                    str1.insert(j,'.')
                    str1.insert(i,'.')
                    str1=''.join(str1)
                    if self.isip(str1):
                        ip_list.append(str1)
        return ip_list
    def isip(self,ip):
        ip = ip.split('.')
        bool =True
        if len(ip) !=4:
            bool = not bool
            return bool
        else:
            for i in ip:
                iv = int(i)
                if iv>255:
                    bool = not bool
                    return bool
                if len(i)>=2 and i[0]=='0':
                    bool = not bool
                    return bool
            return bool

全部评论

相关推荐

07-10 11:08
门头沟学院 Java
Sairus:我注册都注册不了提醒我手机号二次啥的,果然对于人才推得就是快,像我投完了就没回音的
投递京东等公司9个岗位
点赞 评论 收藏
分享
程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
06-25 09:33
厦门大学 Java
程序员饺子:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司9个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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