-
class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> ans = new ArrayList<>(); backtrace(candidates, target, 0, new ArrayList<>(), ans); return ans; } private void backtrace(int[] candidates, int target, int index, List<Integer> path, List<List<Integer>> ans) { if (target == 0) { ans.add(new ArrayList<>(path)); } for (int i = index; i < candidates.length; i++) { if (target - candidates[i] < 0) { break; } path.add(candidates[i]); backtrace(candidates, target - candidates[i], i, path, ans); path.remove(path.size() - 1); } } }
-
class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { Arrays.sort(candidates); List<List<Integer>> ans = new ArrayList<>(); backtrace(candidates, target, 0, new ArrayList<>(), ans); return ans; } private void backtrace(int[] candidates, int target, int index, List<Integer> path, List<List<Integer>> ans) { if (target == 0) { ans.add(new ArrayList<>(path)); } for (int i = index; i < candidates.length; i++) { if (target - candidates[i] < 0) { break; } if (i > index && candidates[i] == candidates[i - 1]) { continue; } path.add(candidates[i]); backtrace(candidates, target - candidates[i], i + 1, path, ans); path.remove(path.size() - 1); } } }
-
class Solution { public List<List<Integer>> permute(int[] nums) { Arrays.sort(nums); int length = nums.length; boolean[] used = new boolean[length]; Map<Integer, Integer> map = new HashMap<>(); List<List<Integer>> ans = new ArrayList<>(); backtrace(nums, used, new ArrayList<>(), ans); return ans; } private void backtrace(int[] nums, boolean[] used, List<Integer> path, List<List<Integer>> ans) { if (path.size() == nums.length) { ans.add(new ArrayList<>(path)); } for (int i = 0; i < nums.length; i++) { if (used[i]) { continue; } used[i] = true; path.add(nums[i]); backtrace(nums, used, path, ans); used[i] = false; path.remove(path.size() - 1); } } }
-
class Solution { public List<List<Integer>> permuteUnique(int[] nums) { Arrays.sort(nums); int length = nums.length; boolean[] used = new boolean[length]; List<List<Integer>> ans = new ArrayList<>(); backtrace(nums, used, new ArrayList<>(), ans); return ans; } private void backtrace(int[] nums, boolean[] used, List<Integer> path, List<List<Integer>> ans) { if (path.size() == nums.length) { ans.add(new ArrayList<>(path)); } 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; path.add(nums[i]); backtrace(nums, used, path, ans); used[i] = false; path.remove(path.size() - 1); } } }
class Solution { public List<List<Integer>> permuteUnique(int[] nums) { int m = 0; int n = nums.length; int[] used = new int[n]; for (int i = 0; i < n; i++) { int j; for (j = 0; j < m; j++) { if (nums[j] == nums[i]) { used[j]++; break; } } if (j == m) { used[m] = 1; nums[m++] = nums[i]; } } List<List<Integer>> ans = new ArrayList<>(); backtrace(n, nums, used, new ArrayList<>(), ans); return ans; } private void backtrace(int n, int[] nums, int[] used, List<Integer> path, List<List<Integer>> ans) { if (path.size() == n) { ans.add(new ArrayList<>(path)); } for (int i = 0; i < used.length; i++) { if (used[i] <= 0) { continue; } used[i]--; path.add(nums[i]); backtrace(n, nums, used, path, ans); used[i]++; path.remove(path.size() - 1); } } }
-
class Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer>> ans = new ArrayList<>(); backtrace(n, k, 1, new ArrayList<>(), ans); return ans; } private void backtrace(int n, int k, int index, List<Integer> path, List<List<Integer>> ans) { if (path.size() + (n - index + 1) < k) { return; } if (k == path.size()) { ans.add(new ArrayList<>(path)); return; } for (int i = index; i <= n; i++) { path.add(i); backtrace(n, k, i + 1, path, ans); path.remove(path.size() - 1); } } }
-
class Solution { public List<List<Integer>> subsets(int[] nums) { Arrays.sort(nums); List<List<Integer>> ans = new ArrayList<>(); backtrace(nums, 0, new ArrayList<>(), ans); return ans; } private void backtrace(int[] nums, int index, List<Integer> path, List<List<Integer>> ans) { ans.add(new ArrayList<>(path)); for (int i = index; i < nums.length; i++) { path.add(nums[i]); backtrace(nums, i + 1, path, ans); path.remove(path.size() - 1); } } }
-
class Solution { public List<List<Integer>> subsetsWithDup(int[] nums) { Arrays.sort(nums); List<List<Integer>> ans = new ArrayList<>(); backtrace(nums, 0, new ArrayList<>(), ans); return ans; } private void backtrace(int[] nums, int index, List<Integer> path, List<List<Integer>> ans) { ans.add(new ArrayList<>(path)); for (int i = index; i < nums.length; i++) { if (i > index && nums[i] == nums[i - 1]) { continue; } path.add(nums[i]); backtrace(nums, i + 1, path, ans); path.remove(path.size() - 1); } } }
-
class Solution { public List<String> letterCasePermutation(String S) { char[] chars = S.toCharArray(); List<String> ans = new ArrayList<>(); backtrace(chars, 0, ans); return ans; } private void backtrace(char[] chars, int index, List<String> ans) { if (index == chars.length) { ans.add(new String(chars)); return; } backtrace(chars, index + 1, ans); if (Character.isLetter(chars[index])) { chars[index] ^= 1 << 5; backtrace(chars, index + 1, ans); } } }
-
class Solution { public String[] permutation(String s) { int n = s.length(), m = 0; int[] used = new int[n]; char[] chars = new char[n]; for (char c : s.toCharArray()) { int i; for (i = 0; i < m; i++) { if (chars[i] == c) { used[i]++; break; } } if (i == m) { used[m] = 1; chars[m++] = c; } } char[] path = new char[n]; List<String> ans = new ArrayList<>(); backtrace(n, m, chars, used, 0, path, ans); return ans.toArray(new String[ans.size()]); } private void backtrace(int n, int m, char[] chars, int[] used, int index, char[] path, List<String> ans) { if (index == n) { ans.add(new String(path)); } for (int i = 0; i < m; i++) { if (used[i] <= 0) { continue; } used[i]--; path[index] = chars[i]; backtrace(n, m, chars, used, index + 1, path, ans); used[i]++; } } }
class Solution { public String[] permutation(String s) { char[] chars = s.toCharArray(); List<String> list = new ArrayList<>(); backtrace(chars, 0, list); return list.toArray(new String[0]); } private void backtrace(char[] chars, int index, List<String> list) { if (index == chars.length - 1) { list.add(new String(chars)); } Set<Character> set = new HashSet<>(); for (int i = index; i < chars.length; i++) { if (set.contains(chars[i])) { continue; } set.add(chars[i]); swap(chars, i, index); backtrace(chars, index + 1, list); swap(chars, i, index); } } private void swap(char[] chars, int i, int j) { char tmp = chars[i]; chars[i] = chars[j]; chars[j] = tmp; } }
回溯法
最新推荐文章于 2024-07-19 19:25:43 发布