题目来源


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. c# windows service

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. [简单]poi word2007表格按模版样式填充行数据

    主要实现了按照模版行的样式填充数据,针对的是动态数据,静态数据可以直接替换变量实现,先说下缺点:1)暂未实现特殊样式填充(如列合并(跨行合并)),只能用于普通样式(如段落间距 缩进 字体 对齐)2)数 ...

  3. poi读写word模板 / java生成word文档

    有一word文档表格 形如: 姓名 ${name} 电话 ${tel} 从数据库读取记录替换上述变量 import java.io.FileOutputStream; import java.util ...

  4. javascript中的function

    function / 对象 所有的变量和方法名的:以字母,$ _开头其他随便,尽量使用英文字母命名,见名知意注意点:不允许使用关键字定义变量和方法的名称====函数即方法,方法即函数====百度:ja ...

  5. MySQL复制延时排查

    今天收到报警,提示从库延时,首先当然是上去查看情况,首先查看机器负载,如下: 可以看到使用cpu已经100%,io没有等待.那么查看mysql是什么情况,执行show processlist没有发现任 ...

  6. mysql insert插入新形式,再也不需要拼接多重insert啦

    注意一下,不能省略表中的任何字段.包括自增id.而且字段的顺序必须和插入表一致 原理是“表插表” INSERT INTO prod_attr select A.* from ( SELECT AS p ...

  7. iisNS 安装程序的思路

    1. 安装程序的目录和 frontEnd一个目录,通过判断是否存在 common/config/db.php 来验证是否已经安装过,如果已经安装过,该文件会自动生成 2.  如果没有安装过,则跳转到 ...

  8. jQuery前端验证多种方式

    JQuery Validate使用总结:一.导入js库<script src="../js/jquery.js" type="text/javascript&quo ...

  9. 放到u-boot/arch/arm/inlcude下面解压A20固件库制作笔记

    运行 build_dragonboard.sh,完成一次编译,首次编译需要消耗 20 分钟以上的时间.这里包括编译bootloader.kernel.rootfs. 修改 Linux 内核配置$ cd ...

  10. HttpStatusCodeResult

    HttpStatusCodeResult:让mvc回传特定的http状态代码与消息给客户端,对于一些特殊的http响应,可利用httpStatusCodeResult帮助我们响应适当的状态代码: 1X ...