24.代码随想录算法训练营第二十四天|93. 复原 IP 地址,78. 子集,90. 子集 II
93. 复原 IP 地址 - 力扣(LeetCode)
有效 IP 地址 正好由四个整数(每个整数位于 0
到 255
之间组成,且不能含有前导 0
),整数之间用 '.'
分隔。
- 例如:
"0.1.2.201"
和"192.168.1.1"
是 有效 IP 地址,但是"0.011.255.245"
、"192.168.1.312"
和"[email protected]"
是 无效 IP 地址。
给定一个只包含数字的字符串 s
,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,这些地址可以通过在 s
中插入 '.'
来形成。你 不能 重新排序或删除 s
中的任何数字。你可以按 任何 顺序返回答案。
示例 1:
输入:s = "25525511135"
输出:["255.255.11.135","255.255.111.35"]
示例 2:
输入:s = "0000"
输出:["0.0.0.0"]
示例 3:
输入:s = "101023"
输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
提示:
1 <= s.length <= 20
s
仅由数字组成
思想:递归控制的是单条路径上,也就是树的不同层次上的结点。而for循环控制的是同层上所有的切割方式,与递归配合,就能把以他为切割点的所有组合都得到。若某个切割点不满足就代表从他以后作为切割点都不能满足。所以直接跳出for循环。
class Solution {
List<String> res = new ArrayList<>();
public List<String> restoreIpAddresses(String s) {
if (s.length() > 12) return res;
backTrack(s, 0, 0);
return res;
}
public void backTrack(String s,int startIndex,int pointNum){
if(pointNum == 3){
if(isValid(s,startIndex,s.length()-1)){
res.add(s);
}
return;
}
for(int i = startIndex;i < s.length();i++){
if(isValid(s,startIndex,i)){
s = s.substring(0,i+1)+"."+s.substring(i+1);
pointNum++;
backTrack(s,i+2,pointNum);
pointNum--;
s = s.substring(0,i+1)+s.substring(i+2);
}else{
break;
}
}
}
private boolean isValid(String 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++){
if(s.charAt(i) > '9' && s.charAt(i) < '0'){
return false;
}
num = num*10 + (s.charAt(i) - '0');
if(num > 255){
return false;
}
}
return true;
}
}
78. 子集 - 力扣(LeetCode)
给你一个整数数组 nums
,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
示例 1:
输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
示例 2:
输入:nums = [0]
输出:[[],[0]]
提示:
1 <= nums.length <= 10
-10 <= nums[i] <= 10
nums
中的所有元素 互不相同
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new LinkedList<>();
public List<List<Integer>> subsets(int[] nums) {
backtracking(nums,0);
return res;
}
public void backtracking(int[] nums,int start){
res.add(new ArrayList(path));
if(start >= nums.length){
return;
}
for(int i = start;i < nums.length;i++){
path.add(nums[i]);
backtracking(nums,i+1);
path.removeLast();
}
}
}
90. 子集 II - 力扣(LeetCode)
给你一个整数数组 nums
,其中可能包含重复元素,请你返回该数组所有可能的 子集(幂集)。
解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
示例 1:
输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
示例 2:
输入:nums = [0]
输出:[[],[0]]
提示:
1 <= nums.length <= 10
-10 <= nums[i] <= 10
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new LinkedList<>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
backtracking(nums,0);
return res;
}
public void backtracking(int[] nums,int start){
res.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.add(nums[i]);
backtracking(nums,i+1);
path.removeLast();
}
}
}
注意:第一层for循环是对树的同层进行处理,递归是对树枝进行处理