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代码如下:

 1 class RestoreIPAddress:
 2
 3     def isValid(self, src):
 4         if src[0] == ‘0‘:
 5             return src == ‘0‘
 6         return 255 >= int(src) > 0
 7
 8     def dfs(self, src, temp, res, count):
 9         if count == 3 and self.isValid(src):
10             res.append(temp + src)
11             return
12         for i in range(1, min(4, len(src))):
13             sub = src[0:i]
14             if self.isValid(sub):
15                 self.dfs(src[i:], temp + sub + ‘.‘, res, count + 1)
16
17     def restoreIpAddresses(self, s):
18         if len(s) < 4 or len(s) > 12:
19             return []
20         res = []
21         self.dfs(s, "", res, 0)
22         # print(res)
23         return res
24
25 p = RestoreIPAddress()
26 p.restoreIpAddresses("0000")
时间: 2024-10-17 12:11:52

LeetCode——Restore IP Addresses的相关文章

LeetCode: Restore IP Addresses [093]

[题目] 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) [题意] 给定一个字

[LeetCode]Restore IP Addresses

题目:Restore IP Addresses 将数字串转换成合法的IP地址,返回所有可能的情况. 思路: 移动3个地址分隔符".",将地址换分成四份,检查是否合法: 注意不能有0开头的地址段(它是非法的),且不用将开头的0去掉. 中间的点从第一个点的后面开始向尾部移动,当第一个点与第二个点的距离大于3时,第一个点向后移动一步,第二个点还原到当前第一个点的后面: 当第一个点移动到最后一个点的前2个位置,则,最后一个点向前移动一步,第一个点和第二个点还原: 最后一个点到串尾距离大于3则退

[LeetCode] Restore IP Addresses 复原IP地址

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) 这道题要求是复原IP地址,IP地

[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) Hide Tags Backtr

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

【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) 题解:深度优先搜索.用resul

Restore IP Addresses leetcode java

题目: 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) 题解: 利用循环递归解

Leetcode题解(2):L93/Restore IP Addresses

L93: 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 n

【leetcode】Restore IP Addresses

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 mat