排列组合类

2017-07-12  本文已影响43人  爱吃虾的雅典娜
/*排列与组合专题:

排列与元素的顺序有关
组合与顺序无关

permutation: 排列 
combination: 组合 在组合之前,需要排序

*/

public class cp {
    /* 
        LeetCode 46. Permutations
        Given a collection of distinct numbers, return all possible permutations.
        For example, [1,2,3] have the following permutations:
        [
          [1,2,3],
          [1,3,2],
          [2,1,3],
          [2,3,1],
          [3,1,2],
          [3,2,1]
        ]
    */
    public List<List<Integer>> permute (int[] nums) {
        // variable of result
        List<List<Integer>> result = new ArrayList<>();
        // corner 
        if(nums.length == 0 || nums == null) {
            return result;
        }
        // sub-result
        List<Integer> list = new ArrayList<>();
        // recursive
        helper(nums, result, list);
        // return
        return result;
    }

    private void helper (int[] nums, List<List<Integer>> result, List<Integer> list) {
        // exit
        if(list.size == nums.length) {
            result.add(new ArrayList(list));
            return;
        }
        // for loop
        for(int i = 0; i < nums.length; i++) {
            if(list.contains(nums[i])) {
                continue;
            }
            // add 
            list.add(nums[i]);
            // recursive
            helper(nums, result, list);
            // resert
            list.remove(list.size()-1);
        }
    }

    /* 
        LeetCode 47. Permutations II
        Given a collection of numbers that might contain duplicates, return all possible unique permutations.
        For example, [1,1,2] have the following unique permutations:
        [
          [1,1,2],
          [1,2,1],
          [2,1,1]
        ]
    */
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        if(nums.length == 0 || nums == null) {
            return result;
        }
        List<Integer> list = new ArrayList<>();
        int[] visited = new int[nums.length];
        Arrays.sort(nums);
        helper(nums, result, list, visited);
        return result;
    }

    private void helper(int[] nums, List<List<Integer>> result, 
                        List<Integer> list, int[] visited) {
        if(list.size() == nums.length) {
            result.add(new ArrayList(list));
            return;
        }
        for(int i = 0; i < nums.length; i ++) {
            if(visited[i] == 1 || (i != 0 && nums[i] == nums[i-1] && visited[i-1] == 0)) {
                continue;
            }
            visited[i] = 1;
            list.add(nums[i]);
            helper(nums, result, list, visited);
            list.remove(list.size() - 1);
            visited[i] = 0;
        }
    }

    /*
        LeetCode 77. Combinations
        Given two integers n and k, 
        return all possible combinations of k numbers out of 1 ... n.
        For example, if n = 4 and k = 2, a solution is:
        [
          [2,4],
          [3,4],
          [2,3],
          [1,2],
          [1,3],
          [1,4],
        ]
    */
    public List<List<Integer>> combinations (int n, int k) {
        List<List<Integer>> result = new ArrayList<>();
        if(n < k || n == 0 || k == 0) {
            return result;
        }
        List<Integer> solution = new ArrayList<>();
        helper(n, k, 1, result, solution);
        return result;
    }

    private void helper(int n, int k, int start, 
                        List<List<Integer>> result,
                        List<Integer> solution) {
        if(solution.size() == k) {
            result.add(new ArrayList(solution));
            return;
        }
        for(int i = start; i <= n; i ++) {
            solution.add(i);
            helper(n, k, i + 1, result, solution);
            solution.remove(solution.size() - 1);
        }
    }

    /*
        LeetCode 39. Combination Sum
        Given a set of candidate numbers (C) 
        (without duplicates) and a target number (T), 
        find all unique combinations in C 
        where the candidate numbers sums to T.
        
        The same repeated number may be chosen from 
        C unlimited number of times.

        Note:
        All numbers (including target) will be positive integers.
        The solution set must not contain duplicate combinations.
        For example, given candidate set [2, 3, 6, 7] and target 7, 
        A solution set is: 
        [
          [7],
          [2, 2, 3]
        ]
    */
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<>();
        if (candidates == null || candidates.length == 0) {
            return result;
        }
        List<Integer> combination = new ArrayList<>();
        Arrays.sort(candidates);
        dfs(candidates, target, 0, combination, result);
        return result;
    }

    public static void dfs(int[] candidates, int target, int index, 
                            List<Integer> combination,
                            List<List<Integer>> result) {
        if (target == 0) {
            result.add(new ArrayList<Integer>(combination));
            return;
        }
        for (int i = index; i < candidates.length; i ++) {
            if (candidates[i] > target) {
                break;
            }
//          if (i != index && candidates[i] == candidates[i-1]) {
//              continue;
//          }
            combination.add(candidates[i]);
            dfs(candidates, target - candidates[i], i, combination, result);
            combination.remove(combination.size()-1);
        }
    }

    /*
        40. Combination Sum II
        Given a collection of candidate numbers (C) and a target number (T), 
        find all unique combinations in C where the candidate numbers sums to T.
        Each number in C may only be used once in the combination.
        Note:
            All numbers (including target) will be positive integers.
            The solution set must not contain duplicate combinations.
            For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8, 
            A solution set is: 
            [
              [1, 7],
              [1, 2, 5],
              [2, 6],
              [1, 1, 6]
            ]
    */
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> combination = new ArrayList<>();
        if (candidates == null) {
            return result;
        }
        Arrays.sort(candidates);
        dfs(candidates, target, 0, combination, result);
        return result;
    }

    public static void dfs(int[] candidates, int target, int index, 
                           List<Integer> combination,
                           List<List<Integer>> result) {
        if (target == 0) {
            result.add(new ArrayList<Integer>(combination));
            return;
        }
        for (int i = index; i < candidates.length; i ++) {
            if (target < candidates[i]) {
                break;
            }
            if (i != index && candidates[i] == candidates[i-1]) {
                continue;
            }
            combination.add(candidates[i]);
            dfs(candidates, target - candidates[i], i + 1, combination, result);
            combination.remove(combination.size()-1);
        }
    }

    /*
        216. Combination Sum III
        Find all possible combinations of k numbers 
        that add up to a number n, 
        given that only numbers from 1 to 9 can be used 
        and each combination should be a unique set of numbers.

        Example 1: Input:  k = 3, n = 7
                   Output: [[1,2,4]]
        Example 2: Input:  k = 3, n = 9
                   Output: [[1,2,6], [1,3,5], [2,3,4]]
    */
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> combination = new ArrayList<>();
        if (k > n || n == 0 || k == 0) {
            return result;
        }
        dfs(k, n, 1, result, combination);
        return result;
    }

    public void dfs(int k, int n, int index, List<List<Integer>> result, List<Integer> combination) {
        if (n == 0 && combination.size() == k) {
            result.add(new ArrayList<Integer>(combination));
            return;
        }
        for (int i = index; i <= 9; i ++) {
            combination.add(i);
            dfs(k, n-i, i+1, result, combination);
            combination.remove(combination.size()-1);
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读