1 Star 0 Fork 0

Neo_1001/LeetCode刷题记录

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LC_93.java 1.43 KB
一键复制 编辑 原始数据 按行查看 历史
Neo_1001 提交于 2024-04-10 22:16 . first commit
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;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/neo1001/leet-code-practice-record.git
git@gitee.com:neo1001/leet-code-practice-record.git
neo1001
leet-code-practice-record
LeetCode刷题记录
master

搜索帮助