题目列表
解题过程
1、93.复原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 地址。
给定一个只包含数字的字符串 s
,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,这些地址可以通过在 s
中插入 '.'
来形成。你 不能 重新排序或删除 s
中的任何数字。你可以按 任何 顺序返回答案。
思路: 和切割回文串很像,就是需要更多的判断,这个有点难处理。而且,地址都是切分成四段,所以要以分割的段数作为终止条件。
class Solution {
List<String> res = new ArrayList<>();
public List<String> restoreIpAddresses(String s) {
if (s.length() > 12) { //小剪枝
return res;
}
backTracking(s, 0, 0);
return res;
}
//startIndex是搜索的起始位置、pointNum是添加.的数量
public void backTracking(String s, int startIndex, int pointNum) {
if (pointNum == 3) {
if (isValidAddress(s, startIndex, s.length() - 1)) {
res.add(s);
}
return;
}
for (int i = startIndex; i < s.length(); i++) {
if (isValidAddress(s, startIndex, i)) {
s = s.substring(0, i + 1) + "." + s.substring(i + 1); //直接在s中改,添加.
pointNum++;
backTracking(s, i + 2, pointNum);
pointNum--; //回溯
s = s.substring(0, i + 1) + s.substring(i + 2); //删掉.
}
}
}
//判断是否为有效的地址段,左闭右闭区间[startIndex, end]
public boolean isValidAddress(String s, int startIndex, int end) {
if (startIndex > end) {
return false;
}
if (startIndex == end) { //只有1位
return true;
} else if (s.charAt(startIndex) == '0') { //前导0
return false;
}
int num = 0; //字符串 -> 数字
for (int i = startIndex; 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;
}
}
2、78.子集
给你一个整数数组 nums
,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
思路: 如果把 子集问题、组合问题、分割问题都抽象为一棵树的话,那么组合问题和分割问题都是收集树的叶子节点,而子集问题是找树的所有节点。
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> subsets(int[] nums) {
backTracking(nums, 0);
return res;
}
public void backTracking(int[] nums, int startIndex) {
res.add(new ArrayList(path));
if (startIndex >= nums.length) {
return;
}
for (int i = startIndex; i < nums.length; i++) {
path.add(nums[i]);
backTracking(nums, i + 1);
path.removeLast();
}
}
}
3、90.子集II
给你一个整数数组 nums
,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
思路: 树枝不去重,树层去重。
使用used数组
class Solution {
LinkedList<Integer> path = new LinkedList<>();
List<List<Integer>> res = new ArrayList<>();
//标记数组
boolean[] used;
int sum = 0;
public List<List<Integer>> subsetsWithDup(int[] nums) {
used = new boolean[nums.length];
//标记数组、初始都赋值为false
Arrays.fill(used, false);
Arrays.sort(nums);
backTracking(nums, 0);
return res;
}
public void backTracking(int[] nums, int startIndex) {
res.add(new ArrayList(path));
if (startIndex >= nums.length) {
return;
}
for (int i = startIndex; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
continue;
}
used[i] = true;
path.add(nums[i]);
backTracking(nums, i + 1);
used[i] = false;
path.removeLast();
}
}
}
不使用used数组
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort( nums );
subsetsWithDupHelper( nums, 0 );
return res;
}
private void subsetsWithDupHelper(int[] nums, int start ) {
res.add(new ArrayList<>(path));
for (int i = start; i < nums.length; i++) {
// 跳过当前树层使用过的、相同的元素
if (i > start && nums[i - 1] == nums[i]) {
continue;
}
path.add(nums[i]);
subsetsWithDupHelper(nums, i + 1 );
path.removeLast();
}
}
}