回溯法

  • 39. 组合总和

    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);
            }
        }
    }
    
  • 40. 组合总和 II

    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);
            }
        }
    }
    
  • 46. 全排列

    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);
            }
        }
    }
    
  • 47. 全排列 II

    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);
            }
        }
    }
    
  • 77. 组合

    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);
            }
        }
    }
    
  • 78. 子集

    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);
            }
        }
    }
    
  • 90. 子集 II

    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);
            }
        }
    }
    
  • 784. 字母大小写全排列

    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);
            }
        }
    }
    
  • 剑指 Offer 38. 字符串的排列

    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;
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值