题目汇总
- LeetCode_78:子集
- LeetCode_90:子集II
- LeetCode_46:全排列
- LeetCode_47:全排列II
- LeetCode_39:组合总和
- LeetCode_40:组合总和II
- LeetCode_131:分割回文串
- LeetCode_77:组合
解题关键点
- 这些题的共同之处在于都可用统一的代码模板规范性的求解;
- 不同之处在于递归结束条件以及下探到下一层所要传递的参数不同。
目前已知运行最为高效的代码
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
backtrack(list, new ArrayList<Integer>(), nums, 0);
return list;
}
private static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums, int start) {
list.add(new ArrayList<Integer>(templist));
for (int i = start; i < nums.length; ++i) {
templist.add(nums[i]);
backtrack(list, templist, nums, i + 1);
templist.remove(templist.size() - 1);
}
}
}
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(nums);
backtrack(list, new ArrayList<Integer>(), nums, 0);
return list;
}
private static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums, int start) {
list.add(new ArrayList<Integer>(templist));
for (int i = start; i < nums.length; ++i) {
if (i > start && nums[i] == nums[i - 1]) {
continue;
}
templist.add(nums[i]);
backtrack(list, templist, nums, i + 1);
templist.remove(templist.size() - 1);
}
}
}
class Solution {
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
backtrack(list, new ArrayList<Integer>(), nums);
return list;
}
private static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums) {
if (templist.size() == nums.length) {
list.add(new ArrayList<Integer>(templist));
return;
} else {
for (int i = 0; i < nums.length; ++i) {
if (templist.contains(nums[i])) {
continue;
}
templist.add(nums[i]);
backtrack(list, templist, nums);
templist.remove(templist.size() - 1);
}
}
}
}
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(nums);
backtrack(list, new ArrayList<Integer>(), nums, new boolean[nums.length]);
return list;
}
private static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums, boolean[] used) {
if (templist.size() == nums.length) {
list.add(new ArrayList<Integer>(templist));
return;
} else {
for (int i = 0; i < nums.length; ++i) {
if (used[i] || i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
continue;
}
used[i] = true;
templist.add(nums[i]);
backtrack(list, templist, nums, used);
used[i] = false;
templist.remove(templist.size() - 1);
}
}
}
}
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
backtrack(list, new ArrayList<Integer>(), candidates, target, 0);
return list;
}
private static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums, int remain, int start) {
if (remain < 0) {
return;
} else if (remain == 0) {
list.add(new ArrayList<Integer>(templist));
return;
} else {
for (int i = start; i < nums.length; ++i) {
templist.add(nums[i]);
backtrack(list, templist, nums, remain - nums[i], i);
templist.remove(templist.size() - 1);
}
}
}
}
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
backtrack(list, new ArrayList<Integer>(), candidates, target, 0);
return list;
}
private static void backtrack(List<List<Integer>> list, List<Integer> templist, int[] nums, int remain, int start) {
if (remain < 0) {
return;
} else if (remain == 0) {
list.add(new ArrayList<Integer>(templist));
return;
} else {
for (int i = start; i < nums.length; ++i) {
if (i > start && nums[i] == nums[i - 1]) {
continue;
}
templist.add(nums[i]);
backtrack(list, templist, nums, remain - nums[i], i + 1);
templist.remove(templist.size() - 1);
}
}
}
}
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> list = new ArrayList<List<String>>();
backtrack(list, new ArrayList<String>(), s, 0);
return list;
}
private static void backtrack(List<List<String>> list, List<String> templist, String s, int start) {
int len = s.length();
if (start == len) {
list.add(new ArrayList<String>(templist));
return;
} else {
for (int i = start; i < len; ++i) {
if (isPalindrome(s, start, i)) {
templist.add(s.substring(start, i + 1));
backtrack(list, templist, s, i + 1);
templist.remove(templist.size() - 1);
}
}
}
}
private static boolean isPalindrome(String s, int left, int right) {
while (left < right) {
if (s.charAt(left++) != s.charAt(right--)) return false;
}
return true;
}
}
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
backtrack(n, k, 1, list, new ArrayList<Integer>());
return list;
}
private static void backtrack(int n, int remain, int start, List<List<Integer>> list, List<Integer> templist) {
if (remain == 0) {
list.add(new ArrayList<Integer>(templist));
return;
} else {
for (int i = start; i <= n - remain + 1; ++i) {
templist.add(i);
backtrack(n, remain - 1, i + 1, list, templist);
templist.remove(templist.size() - 1);
}
}
}
}