题目链接 : https://leetcode-cn.com/problems/restore-ip-addresses/
题目描述:
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
示例:
输入: "25525511135"
输出: ["255.255.11.135", "255.255.111.35"]
思路:
我们要知道IP
的格式,每位是在0~255
之间,
注意: 不能出现以0
开头的两位以上数字,比如012
,08
...
思路一:暴力法
我们把所有出现可能都列举出来,看是否满足条件.
思路二:回溯算法
代码:
思路一:
class Solution:
def restoreIpAddresses(self, s: str) -> List[str]:
n = len(s)
res = []
# 判读是否满足ip的条件
def helper(tmp):
if not tmp or (tmp[0] == "0" and len(tmp) > 1) or int(tmp) > 255:
return False
return True
# 三个循环,把数字分成四份
for i in range(3):
for j in range(i + 1, i + 4):
for k in range(j + 1, j + 4):
if i < n and j < n and k < n:
tmp1 = s[:i + 1]
tmp2 = s[i + 1:j + 1]
tmp3 = s[j + 1:k + 1]
tmp4 = s[k + 1:]
# print(tmp1, tmp2, tmp3, tmp4)
if all(map(helper, [tmp1, tmp2, tmp3, tmp4])):
res.append(tmp1 + "." + tmp2 + "." + tmp3 + "." + tmp4)
return res
java
class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res = new ArrayList<>();
int n = s.length();
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < i + 4; j++) {
for (int k = j + 1; k < j + 4; k++) {
if (i < n && j < n && k < n) {
String tmp1 = s.substring(0, i + 1);
String tmp2 = s.substring(i + 1, j + 1);
String tmp3 = s.substring(j + 1, k + 1);
String tmp4 = s.substring(k + 1);
if (helper(tmp1) && helper(tmp2) && helper(tmp3) && helper(tmp4))
res.add(tmp1 + "." + tmp2 + "." + tmp3 + "." + tmp4);
}
}
}
}
return res;
}
private boolean helper(String tmp) {
if (tmp == null || tmp.length() == 0 || tmp.length() > 3 || (tmp.charAt(0) == '0' && tmp.length() > 1) || Integer.parseInt(tmp) > 255)
return false;
return true;
}
}
思路二:
class Solution:
def restoreIpAddresses(self, s: str) -> List[str]:
res = []
n = len(s)
def backtrack(i, tmp, flag):
if i == n and flag == 0:
res.append(tmp[:-1])
return
if flag < 0:
return
for j in range(i, i + 3):
if j < n:
if i == j and s[j] == "0":
backtrack(j + 1, tmp + s[j] + ".", flag - 1)
break
if 0 < int(s[i:j + 1]) <= 255:
backtrack(j + 1, tmp + s[i:j + 1] + ".", flag - 1)
backtrack(0, "", 4)
return res
java
class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res = new ArrayList<>();
int n = s.length();
backtrack(0, "", 4, s, res, n);
return res;
}
private void backtrack(int i, String tmp, int flag, String s, List<String> res, int n) {
if (i == n && flag == 0) {
res.add(tmp.substring(0, tmp.length() - 1));
return;
}
if (flag < 0) return;
for (int j = i; j < i + 3; j++) {
if (j < n) {
if (i == j && s.charAt(j) == '0') {
backtrack(j + 1, tmp + s.charAt(j) + ".", flag - 1, s, res, n);
break;
}
if (Integer.parseInt(s.substring(i, j + 1)) <= 255)
backtrack(j + 1, tmp + s.substring(i, j + 1) + ".", flag - 1, s, res, n);
}
}
}
}