题目来源


https://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)


题意分析


Input:一串数字

Output:可能的ip

Conditions:符合0~255


题目思路


用dfs,要注意考虑0.0.0.0的情况,不考虑00.00.00.00这样多个0的


AC代码(Python)

 class Solution(object):
def restoreIpAddresses(self, s):
"""
:type s: str
:rtype: List[str]
"""
def dfs(s, sub, ips, ip):
if sub == 4:
if s == "":
ips.append(ip[1:])
return
for i in range(1,4):
if i <= len(s):
if int(s[:i]) <= 255:
dfs(s[i:], sub + 1, ips, ip+"."+s[:i])
if s[0] == "":
break
ips = []
dfs(s, 0, ips, "")
return ips

[LeetCode]题解(python):093 Restore IP Addresses的更多相关文章

  1. Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

    backtracking and invariant during generating the parathese righjt > left  (open bracket and cloas ...

  2. Java for LeetCode 093 Restore IP Addresses

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

  3. LeetCode(93) Restore IP Addresses

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

  4. LeetCode之“字符串”:Restore IP Addresses

    题目链接 题目要求: Given a string containing only digits, restore it by returning all possible valid IP addr ...

  5. [leetcode.com]算法题目 - Restore IP Addresses

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

  6. 093 Restore IP Addresses 复原IP地址

    给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式.例如:给定 "25525511135",返回 ["255.255.11.135", " ...

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

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

  8. 【leetcode】Restore IP Addresses

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

  9. 【LeetCode】93. Restore IP Addresses

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

随机推荐

  1. 不容易系列之二[HDU2042]

    不容易系列之二 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  2. 【wikioi】1034 家园(最大流+特殊的技巧)

    http://wikioi.com/problem/1034/ 太神了这题. 其实一开始我以为是费用流,但是总感觉不对. 原因是我没看到一句话,特定的时刻到达特定的点!! 也就是说,并不是每艘船每次都 ...

  3. Mysql 学习

    一.ubuntu下mysql的安装: 通过sudo apt-get install mysql-server 完成: 然后可以通过/etc/init.d/mysql 进行start/stop/rest ...

  4. CentOS 下实现两台服务器之间的共享NFS

    NFS的安装配置:centos 5 :yum install nfs-utils portmapcentos 6 :yum install nfs-utils rpcbind yum install ...

  5. hdu 猜数字

    这题的意思是找到最大的n使得m次之内的猜测可以猜到1~n之间的任何值.这里是二分思想的逆过程,1~h个数最多猜测log2(n+1)次(n为奇数),故 n=2^m-1; #include"io ...

  6. linux 2.6.37-3.x.x x86_64

    /* * linux 2.6.37-3.x.x x86_64, ~100 LOC * gcc-4.6 -O2 semtex.c && ./a.out * 2010 sd@fuckshe ...

  7. [转] How to generate multiple outputs from single T4 template (T4 输出多个文件)

    本文转自:http://www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/ Updat ...

  8. 中科大各Linux发行版的更新源列表

    sudo vi /etc/apt/sources.list Raspberry Pi deb http://mirrors.ustc.edu.cn/raspbian/raspbian/ jessie ...

  9. Apache Spark源码走读之10 -- 在YARN上运行SparkPi

    y欢迎转载,转载请注明出处,徽沪一郎. 概要 “spark已经比较头痛了,还要将其运行在yarn上,yarn是什么,我一点概念都没有哎,再怎么办啊.不要跟我讲什么原理了,能不能直接告诉我怎么将spar ...

  10. 做了一个简易的git 代码自动部署脚本

    做了一个简易的git 代码自动部署脚本 http://my.oschina.net/caomenglong/blog/472665 发表于2个月前(2015-06-30 21:08)   阅读(200 ...