[LeetCode]题解(python):093 Restore IP Addresses
题目来源
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的更多相关文章
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- Java for LeetCode 093 Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode(93) Restore IP Addresses
题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...
- LeetCode之“字符串”:Restore IP Addresses
题目链接 题目要求: Given a string containing only digits, restore it by returning all possible valid IP addr ...
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 093 Restore IP Addresses 复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式.例如:给定 "25525511135",返回 ["255.255.11.135", " ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
随机推荐
- BZOJ3837 : [Pa2013]Filary
当m取2时,k至少为$\frac{n}{2}$ 所以在最优解中每个数被选中的概率至少为$\frac{1}{2}$ 每次随机选取一个位置i,计算出其它数与$a_i$的差值,将差值分解质因数 所有质因数中 ...
- (centos)linux下访问双系统windows7文件系统
fdisk -l 无法挂载 NTFS格式的分区:mount: unknown filesystem type ‘ntfs’. 问题: # mount –t ntfs /dev/sdb1 ...
- 【BZOJ】1877: [SDOI2009]晨跑(最小费用最大流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1877 费用流做多了,此题就是一眼题. 拆点表示只能经过一次,容量为1,费用为0. 然后再连边即可,跑 ...
- c#中的partial关键字(转)
c#中的partial关键字 Partial: 这是C#2.0的新特性 Partial关键字起到把一个class分段组合作用,能用于多个接口或一个继承 代码片断: namespace Com ...
- 小问题,小细节要注意(string类型转换为bool类型)
一个表中的推荐字段是bit类型的,添加的时候推荐有两个值,如<asp:RadioButtonList ID="RadioButtonList1" runat="se ...
- Sqlserver 存储过程中结合事务的代码
Sqlserver 存储过程中结合事务的代码 --方式一 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ ...
- ztree学习之异步加载节点(一)
ztreedemo.jsp: <%@ page language="java" import="java.util.*" pageEncoding=&qu ...
- 从prompt输入10个人的年龄放入数组,将十个人的年龄求总和。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Java8 十大新特性详解(转)
本教程将Java8的新特新逐一列出,并将使用简单的代码示例来指导你如何使用默认接口方法,lambda表达式,方法引用以及多重Annotation,之后你将会学到最新的API上的改进,比如流,函数式接口 ...
- 【新产品发布】《GM1001 4~20mA 高精度电流采集模块》
一.主要特性 1.测量精度高达±0.01%FS±0.002mA: 2.采样电阻仅10欧姆(20mA时压降仅0.2V),对被测系统影响 微乎其微: 3.差分输入,可测量正反电流无需改动硬件,使用方便: ...