题解 | 数字字符串转化成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)>12:
            return []
        res, path = [], []
        def dfs(index):
            if index > len(s):
                return
            if index == len(s) and len(path) == 4:
                res.append(".".join(path[:]))
                return
            for i in range(index, len(s)):
                if 0 <= int(s[index:i+1]) <=255:
                    if s[index] == "0" and len(s[index:i+1]) > 1:
                        continue
                    path.append(s[index:i+1])
                    dfs(i+1)
                    path.pop()
        dfs(0)
        return res

全部评论

相关推荐

程序员牛肉:小牛肉来也! 基本破不了局了,我给你的建议是适当放弃秋招,投递大厂的日常实习之后赶明年的春招。 在没有实习的情况下,你的项目经历给面试官的作用就是提供提问点方便面试官来提问八股以及场景题而已。因此你现在简历的写法不太对,要着重突出项目中使用的技术点,而不是像你现在这个写的很宽泛。 最好是“基于xxxx技术解决了xxxx问题,解决了xxxx边缘场景问题”。最好是这样写,方便面试官对你的简历进行提问。 最后的最后,问题其实不在你。今年的秋招确实比较寒冬一点,所以找不到是正常的。要做好打持久战的准备。
Java学习交流
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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