93.复原ip地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
有效的 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 ‘.’ 分隔。
例如:“0.1.2.201” 和 “192.168.1.1” 是 有效的 IP 地址,但是 “0.011.255.245”、“192.168.1.312” 和 “192.168@1.1” 是 无效的 IP 地址。
思路:
可以使用stringbuilder记录,也可以在原来的字符串上添加。
substring用法:
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)
问题:
1.开始写的时候没有考虑回溯过程中也要把“.”去掉的问题。
解决这个问题之后,基本上用自己的思路把这道题做出来了。
注意回溯的时候要删除掉哪几个字符。
2.注意int的取值范围。需要使用一定方法进行限制,或者采用题解的做法。
class Solution {
List<String> res = new ArrayList<>();
StringBuilder path = new StringBuilder();
public List<String> restoreIpAddresses(String s) {
restoreIpAddresses1(s, 0, 0);
return res;
}
public void restoreIpAddresses1(String s, int start, int times){
if(start >= s.length() && times >= 4){
res.add(path.toString());
return;
}
if(start >= s.length() || times >= 4)
return;
for(int i = start; i < s.length(); i++){
if(isValid(s,start, i)){
path.append(s.substring(start, i + 1));
if(times < 3){
path.append(".");
}
}else{
continue;
}
restoreIpAddresses1(s, i + 1, times + 1);
path.delete(start + times, i + times + 2);
}
}
/*public boolean isValid(String s, int start, int end){
if(end - start >= 3)
return false;
if(s.charAt(start) == '0'&& start != end)
return false;
String str = s.substring(start, end + 1);
int num = Integer.parseInt(str);
if(num > 255 || num < 0)
return false;
return true;
}
*/
private boolean isValid(String s, int start, int end) {
if (start > end) {
return false;
}
if (s.charAt(start) == '0' && start != end) { // 0开头的数字不合法
return false;
}
int num = 0;
for (int i = start; i <= end; i++) {
if (s.charAt(i) > '9' || s.charAt(i) < '0') { // 遇到⾮数字字符不合法
return false;
}
num = num * 10 + (s.charAt(i) - '0');
if (num > 255) { // 如果⼤于255了不合法
return false;
}
}
return true;
}
}
78.子集问题
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
说明:解集不能包含重复的子集。
示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
**思路:**与前面题目的差别在于所有path都记录下来
**注意:**i从start开始,而不是0;记得写返回值。
class Solution {
List<List<Integer>> list = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> subsets(int[] nums) {
subsets1(nums, 0);
return list;
}
public void subsets1(int[] nums, int start){
list.add(new ArrayList(path));
if(path.size() >= nums.length )
return;
for(int i = start; i < nums.length; i ++){
path.push(nums[i]);
subsets1(nums, i + 1);
path.pop();
}
}
}
90.子集2
给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
说明:解集不能包含重复的子集。
输入: [1,2,2]
输出: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
思路:与上面题目差别在于如何去除重复
不是一个新的开始,并且还等于上一个,那么本轮就不需要考虑了。
class Solution {
List<List<Integer>> list = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
subsets1(nums, 0);
return list;
}
public void subsets1(int[] nums, int start){
list.add(new ArrayList(path));
if(start >= nums.length )
return;
for(int i = start; i < nums.length; i ++){
if(i != start && nums[i] == nums[i-1]){
continue;
}
path.push(nums[i]);
subsets1(nums, i + 1);
path.pop();
}
}
}