[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 ...
随机推荐
- BZOJ4320 : ShangHai2006 Homework
取$M=\sqrt{300000}$. 设$g[i]$表示程序员的$\bmod i$最小的值. 若$Y<M$,那么可以在$O(M)$时间内完成对所有$g[i]$的修改,$O(1)$时间内完成查询 ...
- vim molokai配色方案(调过)
" Vim color file " " Author: Tomas Restrepo <tomas@winterdom.com> " " ...
- 发布android app到android market的方法
转载自: http://www.stwind.org/android-market 给你的程序签名注意事项:所有提交到Market的程序必须经过签名.未经签名的程序不能安装.你可以使用个人证书去签 ...
- C#操作XML(读XML,写XML,更新,删除节点,与dataset结合等)【转载】
已知有一个XML文件(bookstore.xml)如下: Corets, Eva 5.95 1.插入节点 往节点中插入一个节点: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- js上传图片预览
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- MIB-II
. 1.3.6.1.2.1
- eclipse java工程和maven工程的互相转换
eclipse java工程和maven工程的互相转换 maven转为工程一般工程: 工程右键--->Maven--->Disable Maven Nature转为一般工程. 一般工程转为 ...
- Powershell的远程管理
powershell有强大的远程管理功能,但是现在遇到个问题,我们之前的客户端操作系统都是默认安装的,没做默认设置,请问如何通过gpo将所有和远程有关的设置都搞定啊?到底要设置哪些个选项? 我的环 ...
- gridview header增加排序图标
/* add sorting icons to gridview sort links */a.asc:after, a.desc:after { position: relative; top: 1 ...
- java字符串抉择
下面我们就字符串连接方面分析. 1.String 打开String的源码,如图所示 会发现存储字符串的字符数值是final常量.再看String的构造方法,发现String的value值在构造方法就确 ...