Day24 回溯算法part03
93.复原IP地址
本期本来是很有难度的,不过 大家做完 分割回文串 之后,本题就容易很多了
题目链接/文章讲解:代码随想录
视频讲解:回溯算法如何分割字符串并判断是合法IP?| LeetCode:93.复原IP地址_哔哩哔哩_bilibili
class Solution {
private List<String> result = new ArrayList<>();
public List<String> restoreIpAddresses(String s) {
if(s.length() > 12 || s.length() < 4){
return result;
}
restoreIp(new StringBuilder(s), 0, 3);
return result;
}
public void restoreIp(StringBuilder sb , int starindex , int pointnum){
if(pointnum == 0){
if(isValid(sb, starindex, sb.length() - 1)){
result.add(sb.toString());
return;
}
}
for(int i = starindex; i < sb.length() ; i++){
if(isValid(sb, starindex, i)){
sb.insert(i+1, '.');
restoreIp(sb, i + 2, pointnum - 1);
sb.deleteCharAt(i+1);
}else{
break;