93.复原IP地址
题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
解题思路:
java:
class Solution {
List<String> result = new ArrayList<String>();
StringBuilder stringBuilder = new StringBuilder();
public List<String> restoreIpAddresses(String s) {
restoreIpAddressesHandler(s, 0, 0);
return result;
}
public void restoreIpAddressesHandler(String s, int start, int number) {
if (start == s.length() && number == 4) {
result.add(stringBuilder.toString());
return;
}
if (start == s.length() || number == 4) {
return;
}
for (int i = start; i < s.length() && i - start < 3 && Integer.parseInt(s.substring(start, i + 1)) >= 0 && Integer.parseInt(s.substring(start, i + 1)) <= 255; i++) {
if (i + 1 - start > 1 && s.charAt(start) - '0' == 0) {
continue;
}
stringBuilder.append(s.substring(start, i + 1));
if (number < 3) {
stringBuilder.append(".");
}
number++;
restoreIpAddressesHandler(s, i + 1, number);
number--;
stringBuilder.delete(start + number, i + number + 2);
}
}
}