题目
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
有效的 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。
例如:"0.1.2.201" 和 "192.168.1.1" 是 有效的 IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 无效的 IP 地址。
示例 1:
输入:s = "25525511135"
输出:["255.255.11.135","255.255.111.35"]
示例 2:
输入:s = "0000"
输出:["0.0.0.0"]
示例 3:
输入:s = "1111"
输出:["1.1.1.1"]
示例 4:
输入:s = "010010"
输出:["0.10.0.10","0.100.1.0"]
示例 5:
输入:s = "101023"
输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
提示:
- 0 <= s.length <= 3000
- s 仅由数字组成
public static List<String> restoreIpAddresses(String str) {
//输入: "25525511135" 输出: ["255.255.11.135", "255.255.111.35"]
return doFindIp(str.toCharArray(), 0, new ArrayList<>(), new ArrayList<>());
}
public static List<String> doFindIp(char[] strs, int index, List<String> res, List<Integer> ips) {
if (index == strs.length && ips.size() == 4) {
res.add(String.join(".", ips.stream().map(val -> val.toString()).collect(Collectors.toList())));
}
if (index == strs.length || ips.size() == 4) {
return res;
}
int newIp = 0;
for (int cursor = index; cursor < strs.length; cursor++) {
newIp = newIp * 10 + (strs[cursor] - '0');
if (newIp < 0 || newIp > 255) {
break;
}
ips.add(newIp);
doFindIp(strs, cursor + 1, res, ips);
ips.remove(ips.size() - 1);
if (newIp == 0) {
break;
}
}
return res;
}