摘要:第一种解法,找出第一部分合法的剩余部分变成相似子问题。这里的特性是最大数字不能超过。比上个方法好的地方在于才会判断数字是否合法,避免了很多这种不需要检查的情况。
</>复制代码
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)
第一种解法,backtracking, 找出第一部分合法的IP, 剩余部分变成相似子问题。
</>复制代码
public class Solution {
public List restoreIpAddresses(String s) {
List res = new ArrayList();
if(s.length() < 4 || s.length() > 12) return res;
dfs(s, "", res, 1);
return res;
}
public void dfs(String s, String temp, List res, int count){
if(count == 4 && isValid(s)){
res.add(temp + s);
return;
}
for(int i = 1; i < Math.min(4, s.length()); i++){
String cur = s.substring(0, i);
if(isValid(cur)){
dfs(s.substring(i), temp + cur + ".", res, count+1);
}
}
}
public boolean isValid(String s){
if(s.charAt(0) == "0") return s.equals("0");
int num = Integer.parseInt(s);
return 0 < num && num < 256;
}
}
2 这里IP的特性是最大数字不能超过255。比上个方法好的地方在于a+b+c+d= s.length()才会判断数字是否合法,避免了很多1+1+1+N这种不需要检查的情况。
</>复制代码
public class Solution {
public List restoreIpAddresses(String s) {
List ret = new ArrayList<>();
StringBuffer ip = new StringBuffer();
for(int a = 1 ; a < 4 ; ++ a)
for(int b = 1 ; b < 4 ; ++ b)
for(int c = 1 ; c < 4 ; ++ c)
for(int d = 1 ; d < 4 ; ++ d)
{
if(a + b + c + d == s.length() )
{
int n1 = Integer.parseInt(s.substring(0, a));
int n2 = Integer.parseInt(s.substring(a, a+b));
int n3 = Integer.parseInt(s.substring(a+b, a+b+c));
int n4 = Integer.parseInt(s.substring(a+b+c));
if(n1 <= 255 && n2 <= 255 && n3 <= 255 && n4 <= 255)
{
ip.append(n1).append(".").append(n2)
.append(".").append(n3).append(".").append(n4);
if(ip.length() == s.length() + 3) ret.add(ip.toString());
ip.delete(0, ip.length());
}
}
}
return ret;
}
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/66922.html
摘要:以剩下的字符串,当前字符串,剩余单元数传入下一次递归。结束条件字符串长度为,并且剩余单元数为 Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given 25525511135, return [2...
摘要:题目要求返回字符串能够组成的所有地址。思路与代码地址由位二进制数字构成,一共分为个区间,每个区间位。那么我们只要划分出这四个区间,然后判断这四个区间的值是否符合标准即可。 题目要求 Given a string containing only digits, restore it by returning all possible valid IP address combinatio...
Problem Given a string containing only digits, restore it by returning all possible valid IP address combinations. Example: Input: 25525511135Output: [255.255.11.135, 255.255.111.35] Solution class So...
摘要:题目描述题目理解将一段字符广度搜索截取,分别有种组合形式,添加限制条件,过滤掉不适合的组合元素。长度,大小,首字母应用如果进行字符串的子元素组合穷举,可以应用。所有的循环,利用到前一个状态,都可以理解为动态规划的一种分支 题目描述:Given a string containing only digits, restore it by returning all possible va...
摘要:第一个分割点第二个分割点第三个分割点 Problem Given a string containing only digits, restore it by returning all possible valid IP address combinations. Example Given 25525511135, return [ 255.255.11.135, 255....
阅读 1315·2021-11-23 09:51
阅读 2062·2021-10-08 10:05
阅读 2424·2019-08-30 15:56
阅读 1967·2019-08-30 15:55
阅读 2716·2019-08-30 15:55
阅读 2563·2019-08-30 13:53
阅读 3566·2019-08-30 12:52
阅读 1392·2019-08-29 10:57