代码拉取完成,页面将自动刷新
package LeetCode;
import java.util.ArrayList;
import java.util.List;
//2024-01-06-16:00
public class LC_93 {
static List<String> res = new ArrayList<>();
public static void main(String[] args) {
restoreIpAddresses("2736786374048");
}
public static List<String> restoreIpAddresses(String s) {
StringBuilder path = new StringBuilder(s);
backtracking(path, 0, 0);
System.out.println(res);
return res;
}
public static void backtracking(StringBuilder s, int startIdx, int pointNums) {
if (pointNums == 3) {
if (isValid(s, startIdx, s.length() - 1)) {
res.add(s.toString());
}
return;
}
for (int i = startIdx; i < s.length(); i++) {
if (isValid(s, startIdx, i)) {
s.insert(i + 1, ".");
backtracking(s, i + 2, pointNums + 1);
s.deleteCharAt(i + 1);
} else break;
}
}
public static boolean isValid(StringBuilder s, int start, int end) {
if (start > end) return false;
if (s.charAt(start) == '0' && start != end) return false;
int num = 0;
for (int i = start; i <= end; i++) {
int digit = s.charAt(i) - '0';
num = num * 10 + digit;
if (num > 255) return false;
}
System.out.println("num:" + s.substring(start, end + 1));
return true;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。