LeetCode——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)
题目是给一个string,划分合理的ip的地址并给出包含所有合理的ip地址的list,我们知道ip地址的组成是4个部分,合理的范围是0.0.0.0~255.255.255.255。
代码主要有两个函数:
isValid——验证是否合理,如果开头是0则这个串必须是0,否则必须大于0小于255;
dfs——使用深搜的方式,搜索所有可能的组合;
所以用DFS的方式,搜索所有可能的组合,每一次搜索至第四个部分的时候,如果也是valid的话,那么这是一个合理的IP,添加到结果列表里
注意一下生成IP的细节, 添加 . 什么的。
Python代码如下:
class RestoreIPAddress:
def isValid(self, src):
if src[0] == '':
return src == ''
return 255 >= int(src) > 0
def dfs(self, src, temp, res, count):
if count == 3 and self.isValid(src):
res.append(temp + src)
return
for i in range(1, min(4, len(src))):
sub = src[0:i]
if self.isValid(sub):
self.dfs(src[i:], temp + sub + '.', res, count + 1)
def restoreIpAddresses(self, s):
if len(s) < 4 or len(s) > 12:
return []
res = []
self.dfs(s, "", res, 0)
# print(res)
return res
p = RestoreIPAddress()
p.restoreIpAddresses("")
LeetCode——Restore IP Addresses的更多相关文章
- 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 @ Python
原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...
- [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 ...
随机推荐
- Swift: The Basics
Swift是类型安全的语言: Swift introduces optional types, which handle the absence of a value. Optional say ei ...
- RT: TCP connection close
CLOSE is an operation meaning "I have no more data to send." The notion of closing a full- ...
- okhttp 常用使用方式 封装 演示
工具介绍 使用: AndroidStudio:[compile 'com.squareup.okhttp3:okhttp:3.4.2']和[compile 'com.zhy:okhttputils:2 ...
- 转载:C# 中的委托
原文地址 http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx 感谢博主分享! 引言 委托和 ...
- 基于bootstrap的轮播广告页,带图片和文字
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...
- css.day05
1. 外边距合并 不是bug 而是一个特性 (以最大的那个边距为准) 两个盒子是并列关系 两个盒子 父子级关系 1. border 2.overflow:hidden; 3. padding ...
- How do I size a UITextView to its content?
UITextView 自适应高度,搬来一篇stack上的: Is there a good way to adjust the size of a UITextView to conform to ...
- form表单中的label标签
小伙伴们,你们在前面学习表单各种控件的时候,有没有发现一个标签--label,这一小节就来揭晓它的作用. label标签不会向用户呈现任何特殊效果,它的作用是为鼠标用户改进了可用性.如果你在 labe ...
- SQL UPDATE 经典
1 sql中用另一个表的一列来更新数据库表 SELECT TOP 1000 [a] ,[b] ,[c] FROM [单元测试项目].[dbo].[A] SELECT TOP 1000 [a] ,[b] ...
- mysql报Fatal error encountered during command execution的解决办法
连接字符串里加上 Allow User Variables=True 解决. 否则时不时的报错,存储过程名长一点也报错,又有时报有时不报,参数传1位数就正常2位数就报错等…… 折腾mysql蛋疼啊