题目来源


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)

  1. class Solution(object):
  2. def restoreIpAddresses(self, s):
  3. """
  4. :type s: str
  5. :rtype: List[str]
  6. """
  7. def dfs(s, sub, ips, ip):
  8. if sub == 4:
  9. if s == "":
  10. ips.append(ip[1:])
  11. return
  12. for i in range(1,4):
  13. if i <= len(s):
  14. if int(s[:i]) <= 255:
  15. dfs(s[i:], sub + 1, ips, ip+"."+s[:i])
  16. if s[0] == "":
  17. break
  18. ips = []
  19. dfs(s, 0, ips, "")
  20. 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. Color the Ball[HDU1199]

    Color the Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. topcoder SRM 591 DIV2 TheArithmeticProgression

    #include <iostream> #include <cstdlib> using namespace std; class TheArithmeticProgressi ...

  3. Interleaving String leetcode java

    题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given ...

  4. asp.net 微信企业号办公系统-流程设计--流程步骤设置-数据设置

    数据设置是控制在流程处理过程中,当前步骤的数据显示与编辑状态,控制当前步骤哪些字段为只读,隐藏或可编辑.需要配合表单设计器使用.

  5. 源码安装Postgresql9.4.1

    1.先到官网下载http://www.postgresql.org/ftp/source/v9.4.1/ tar包 2.解压后执行: sudo apt-get install zlib1g-dev s ...

  6. [转载] c++ cout 格式化输出浮点数、整数及格方法

    C语言里可以用printf(),%f来实现浮点数的格式化输出,用cout呢...? 下面的方法是在网上找到的,如果各位有别的办法谢谢留下... iomanip.h是I/O流控制头文件,就像C里面的格式 ...

  7. [转]OBOUT ASP.NET HTML Editor - Insert HTML

    本文转自:http://www.obout.com/editor_new/sample_InsertHTML.aspx Example demonstrates how to access HTML ...

  8. Responsive布局技巧

    在Responsive布局中,可以毫无保留的丢弃: 第一, 尽量少用无关紧要的div: 第二,不要使用内联元素(inline): 第三,尽量少用JS或flash: 第四,丢弃没用的绝对定位和浮动样式: ...

  9. SQL阻止保存要求重新创建表的更改 在哪里设置

    ef生成的数据表,有数据,设计的时候,想把某个字段改成可为null. 报 “阻止保存要求重新创建表”错误 百度一下: 修改数据库表结构时提示[不允许保存更改.您所做的更改要求删除并重新创建以下表.您对 ...

  10. PHP 开发 APP 接口 学习笔记与总结 - XML 方式封装通信接口

    1.PHP 生成 XML 数据 ① 拼接字符串 ② 使用系统类(DomDocument,XMLWriter,SimpleXML) 例1 使用 PHP 系统类中的 DomDocument 类: < ...