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

数字字符串转化成IP地址

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

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @return string字符串一维数组
#
class Solution:
    def restoreIpAddresses(self , s: str) -> List[str]:
        # write code here
        if len(s) < 4:
            return []
        res = []
        l = len(s)
        for i in range(1,4):
            one = s[0:i]
            if not one or int(one) > 255 or (len(one) > 1 and one.startswith('0')):
                continue
            for j in range(i+1, i+4):
                two = s[i:j]
                if not two or int(two) > 255 or (len(two) > 1 and two.startswith('0')):
                    continue
                for k in range(j+1, j+4):
                    three = s[j:k]
                    if not three or int(three) > 255 or (len(three) > 1 and three.startswith('0')):
                        continue
                    if not s[k:l] or int(s[k:l]) > 255 or (len(s[k:l]) > 1 and s[k:l].startswith('0')):
                        continue
                    res.append('.'.join([one, two, three, s[k:l]]))

        return res

全部评论

相关推荐

牛客928043833号:在他心里你已经是他的员工了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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