[leetcode]Restore IP Addresses @ Python
原题地址:https://oj.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)
解题思路:这个明显是用dfs来解决。来一段精致简练的代码吧。
代码:
class Solution:
# @param s, a string
# @return a list of strings
def restoreIpAddresses(self, s):
def dfs(s, sub, ips, ip):
if sub == 4: # should be 4 parts
if s == '':
ips.append(ip[1:]) # remove first '.'
return
for i in range(1, 4): # the three ifs' order cannot be changed!
if i <= len(s): # if i > len(s), s[:i] will make false!!!!
if int(s[:i]) <= 255:
dfs(s[i:], sub+1, ips, ip+'.'+s[:i])
if s[0] == '': break # make sure that res just can be '0.0.0.0' and remove like '00'
ips = []
dfs(s, 0, ips, '')
return ips
[leetcode]Restore IP Addresses @ Python的更多相关文章
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode——Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] Restore IP Addresses 回溯
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode Restore IP Addresses
DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...
- 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 ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
随机推荐
- Odoo访问权限(一)
Odoo访问权限(一) 四个ODOO权限管理层次 一. Odoo 菜单级别: 即,不属于指定菜单所包含组的用户看不到该菜单.不安全,只是隐藏菜单,若用户知道菜单ID,仍然可以通过指定URL访问 二. ...
- fatal error C1060:compiler is out of heap space
今天svn update了下代码,rebuild工程的时候报错: fatal error C1060:compiler is out of heap space 意思是说编译器堆内存不足 百度结果:V ...
- progress进度条的样式修改
由于公司在做的公众号上需要进度条,我就想着用progress标签,可是progress标签很难修改样式,因而在网上查询了一番. 现在总结一下. progress是H5新标签,主要用于显示进度条.由于他 ...
- BZOJ.1034.[ZJOI2008]泡泡堂(贪心)
题目链接 容易想到田忌赛马.但是是不对的,比如2 3对1 3,按田忌赛马策略会3->1 2->3,但是3->3 2->1显然更优. 而如果按己方最强>=对方最强则开打,也 ...
- hdu 4417 区间内比h小的数 划分树
二分查找最近一个比h小的数 #include<cstdio> #include<iostream> #include<algorithm> #include< ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem I. Interest Targeting 模拟题
Problem I. Interest Targeting 题目连接: http://codeforces.com/gym/100714 Description A unique display ad ...
- CentOS 7下启动postfix服务报错:fatal: parameter inet_interfaces: no local interface found for ::1
sed -i 's/inet_interfaces = localhost/inet_interfaces = all' /etc/postfix/main.cf service postfix re ...
- UIScrollView 遇到的小坑
在做一个 UIScrollView 展示的时候 ,须要计算 contentSize 的高度,于是 我遍历了一下 UIScrollView 全部的子view的高度累加 然后得出 高度 .奇怪的是 发现 ...
- STM32 F4 General-purpose Timers for Periodic Interrupts
STM32 F4 General-purpose Timers for Periodic Interrupts
- ElasticSearch-.net平台下c#操作ElasticSearch详解
ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSea ...