原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/

题意:

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

For example:
Given "25525511135",

return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

解题思路:这个明显是用dfs来解决。来一段精致简练的代码吧。

代码:

class Solution:
# @param s, a string
# @return a list of strings
def restoreIpAddresses(self, s):
def dfs(s, sub, ips, ip):
if sub == 4: # should be 4 parts
if s == '':
ips.append(ip[1:]) # remove first '.'
return
for i in range(1, 4): # the three ifs' order cannot be changed!
if i <= len(s): # if i > len(s), s[:i] will make false!!!!
if int(s[:i]) <= 255:
dfs(s[i:], sub+1, ips, ip+'.'+s[:i])
if s[0] == '': break # make sure that res just can be '0.0.0.0' and remove like '00'
ips = []
dfs(s, 0, ips, '')
return ips

[leetcode]Restore IP Addresses @ Python的更多相关文章

  1. LeetCode: Restore IP Addresses 解题报告

    Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...

  2. [LeetCode] Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  3. LeetCode——Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  4. [LeetCode] Restore IP Addresses 回溯

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  5. LeetCode Restore IP Addresses

    DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...

  6. leetcode 93. Restore IP Addresses(DFS, 模拟)

    题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...

  7. 【leetcode】Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  8. 【LeetCode】93. Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  9. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

随机推荐

  1. 运行程序,解读this指向---case4

    var param = 'window'; var obj1 = { param: 'obj1', fn1: function () { console.log(this.param); }, fn2 ...

  2. C#中四种常用集合的运用(非常重要)

    C#中4个常用的集合 1.ArrayList ArrayList类似于数组,有人也称它为数组列表.ArrayList可以动态维护,而数组的容量是固定的. 它的索引会根据程序的扩展而重新进行分配和调整. ...

  3. 如果想使用GIT Extentions的解决冲突窗口,安装时必须勾选KDIFF3

    因为第一次安装时,没有选择同时安装KDIFF3,所以遇到冲突时,点击合并,始终无法弹出合并窗口. 还有一个问题,就是在安装时,要选择OpenSSH,不要选择PuTTY.

  4. Booting dircetly into Redlink FW from flash

    Booting dircetly into Redlink FW from flash Hello, the usual way to use the Redlink FW is a two-step ...

  5. oracle like 条件拼接

    (1) ibatis xml配置:下面的写法只是简单的转义 namelike '%$name$%' (2) 这时会导致sql注入问题,比如参数name传进一个单引号“'”,生成的sql语句会是:nam ...

  6. WebConfig 加密解密的原理是什么?

    WebConfig 加密解密的原理是什么? 使用命令 加密数据连接串 加密:aspnet_regiis -pef connectionStrings d:/...(webconfig所在路径,不能含中 ...

  7. 从.snk文件导出密钥

    先声明该文的实用性不强, 要产生一对密钥可以有更简单的方法.该文简单解释了.snk文件的格式,并给出了从中提取密钥的C#代码. .snk文件(Strong Name Key)也可以叫签名文件,它一般用 ...

  8. How AOT compares to a traditional JIT compiler

    Ahead-of-Time (AOT) compilation is in contrast to Just-in-Time compilation (JIT). In a nutshell, .NE ...

  9. 使用HTML5和CSS3碎语

    当我们使用HTML5, CSS3,甚至Bootstrap设计网站的时候,有些方面是必须考虑的,比如字体大小,标题大小,行间距,每行字数,字体,颜色,背景图片和文字的搭配,图标,留白和布局......所 ...

  10. ASP.NET Identity系列01,揭开神秘面纱

    早在2005年的时候,微软随着ASP.NET 推出了membership机制,十年磨一剑,如今的ASP.NET Identity是否足够强大,一起来体会. 在VS2013下新建项目,选择"A ...