https://leetcode.cn/problems/restore-ip-addresses/description/
This Java solution is designed to generate all possible valid IP addresses from a given string s
. The code utilizes a depth-first search (DFS) strategy to explore all potential segmentations of the input string into four parts, each representing an octet of an IP address. Let’s break down the implementation and the key components of this solution.
Class Members:
List result
: A list to store the resulting valid IP addresses.static final int SEGCOUNT = 4
: A constant to represent the number of segments in an IP address.int[] SEGMENTS = new int[SEGCOUNT]
: An array to store the current segments of the IP address being formed.Main Method (restoreIpAddresses
):
s
.segNums = 0
) and starting index (segIndex = 0
).DFS Method (dfs_restoreIpAddresses
):
SEGCOUNT
and the end of the string is reached, it forms a valid IP address.public List<String> restoreIpAddresses(String s) {
dfs_restoreIpAddresses(s, 0, 0);
return result;
}
dfs_restoreIpAddresses
method to start the DFS traversal.private void dfs_restoreIpAddresses(String s, int segNums, int segIndex) {
// Base case: If four segments are formed
if (segNums == SEGCOUNT) {
if (segIndex == s.length()) {
StringBuilder ipAddress = new StringBuilder();
for (int i = 0; i < SEGCOUNT; ++i) {
ipAddress.append(SEGMENTS[i]);
if (i < SEGCOUNT - 1) {
ipAddress.append('.');
}
}
result.add(ipAddress.toString());
}
return;
}
// Edge case: If the string is exhausted before forming 4 segments
if (segIndex == s.length()) {
return;
}
// Handling leading zeroes
if (s.charAt(segIndex) == '0') {
SEGMENTS[segNums] = 0;
dfs_restoreIpAddresses(s, segNums + 1, segIndex + 1);
return;
}
// Form segments and proceed with DFS
int addr = 0;
for (int segNext = segIndex; segNext < s.length(); segNext++) {
addr = addr * 10 + (s.charAt(segNext) - '0');
if (addr > 0 && addr <= 255) {
SEGMENTS[segNums] = addr;
dfs_restoreIpAddresses(s, segNums + 1, segNext + 1);
} else {
return;
}
}
}
This algorithm ensures that all valid combinations are checked systematically and invalid cases are pruned early, leading to efficient generation of valid IP addresses from the given string.
package leetcode板块;
import java.util.ArrayList;
import java.util.List;
public class _93复原IP地址_回溯 {
List<String> result = new ArrayList<String>();
static final int SEGCOUNT = 4; //必须要保证有四个部分
int [] SEGMENTS = new int[SEGCOUNT]; // 构建一个数组用做记录每次的IP
/**
* 复原所有可能的IP情况
* @param s
* @return
*/
public List<String> restoreIpAddresses(String s) {
dfs_restoreIpAddresses(s,0,0);
return result;
}
/**
*
* @param s 字符串
* @param segNums ip四个区间的当前数量
* @param segIndex 当前所用到的长度索引位置
*/
private void dfs_restoreIpAddresses(String s, int segNums, int segIndex) {
// 终止条件,且是找到了四个地址
if (segNums == SEGCOUNT){
//查看当前选择下,是否能够满足题意,,,,
//如果当前长度全部使用完毕,则说明满足条件,对于数值的判断就不放在这里进行
if (segIndex == s.length()){
StringBuilder ipAddress = new StringBuilder();
for (int i = 0;i<SEGCOUNT;++i){
ipAddress.append(SEGMENTS[i]);
if (i < SEGCOUNT-1){
//添加 .
ipAddress.append('.');
}
}
result.add(ipAddress.toString());
}
return;
}
//如果是没找到4个地址,但是长度用完了,则直接return
if (segIndex == s.length()){
return;
}
// TODO ---------------- 开始进行一般化处理-----------------
// 1.特殊情况 以 0 开头,就没有其他的方案可供选择,只能0单独作为一个IP地址
if (s.charAt(segIndex) == '0'){
SEGMENTS[segNums] = 0;
dfs_restoreIpAddresses(s,segNums+1,segIndex+1);
}
// 其他情况的话则要进行分类讨论
int addr = 0;
// 需要考虑后面的是否也能够满足条件,不会对别人造成无法满足条件的情况
for (int segNext = segIndex;segNext < s.length();segNext++){
addr = addr * 10 + (s.charAt(segNext) - '0');
if (addr > 0 && addr <= 255){
SEGMENTS[segNums] = addr;
dfs_restoreIpAddresses(s,segNums+1,segNext+1);
}else {
return;
}
}
}
}