排列组合类

/*排列与组合专题:

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

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);
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 从一个范围内获取一组不重复的随机数,返回这个数组: 头文件: /*获取随机数@param count获取随机...
    iOS_Programmer阅读 1,996评论 0 1
  • 排列组合公式 排列组合公式/排列组合计算公式 公式P是指排列,从N个元素取M个进行排列。 公式C是指组合,从N个元...
    魔法师_阅读 202,551评论 1 17
  • 加法原理和乘法原理,是排列组合中的二条基本原理,在解决计数问题中经常运用。掌握这两条原理,并能正确区分他们,至关重...
    勇赴阅读 22,150评论 3 9
  • 学习概率论与数理统计,要用到排列组合的知识,更重要的是要用到排列组合的思维方法,因此,学习概率与统计是很有必要了解...
    勇赴阅读 5,200评论 1 3
  • 《程序员的数学》读书笔记目录 0的作用 罗马计数法 余数的运用 逻辑运算 排列组合 归纳与递归 认清计数对象 工具...
    广州小拳拳阅读 976评论 0 0