栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Permutation 和 Combination

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Permutation 和 Combination

文章目录
    • Permutation
      • 代码
      • 代码核心思路
    • Combination
      • 代码
      • 代码核心思路
    • 总结

Permutation 和 Combination是算法中非常常见的两种数据的排列方式,也就是数学中的排列和组合。

Permutation: 排列,指从给定个数的元素中取出指定个数的元素进行排序。
Combination: 组合,指从给定个数的元素中仅仅取出指定个数的元素,不考虑排序。

本文的主要目的在于总结在算法题中,这两种类型题目的做题模板。虽然题目变化可能是多样的,但是万变不离其宗。


Permutation

以leetcode题目46. Permutations为例。

题目叙述如下:

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Example

Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

代码
class Solution {
    public List> permute(int[] nums) {
        List> ans = new ArrayList<>();
        dfs(ans, new ArrayList<>(), 0, nums);
        return ans;
    }
    
    private void dfs(List> ans, List list, int begin, int[] nums) {
        if (list.size() == nums.length) {
            ans.add(new ArrayList<>(list));
            return;
        }
        
        if (begin >= nums.length) {
            return;
        }
        
        for (int i = begin; i < nums.length; i++) {
            swap(nums, begin, i);
            list.add(nums[begin]);
            dfs(ans, list, begin + 1, nums);
            list.remove(list.size() - 1);
            swap(nums, begin, i);
        }
        return;
    }
    
    private void swap(int[] nums, int a, int b) {
        int tmp = nums[a];
        nums[a] = nums[b];
        nums[b] = tmp;
    }
}
代码核心思路
  1. DFS;
  2. 交换(如题目中的swap)
  3. 保存中间结果(如函数dfs的第二个参数List list )
  4. 保存最终结果(如函数dfs的第一个参数List> ans)
  5. 恢复现场(list.remove(list.size() - 1))

Combination

以leetcode题目combination-sum-iii为例。

题目叙述如下:

Find all valid combinations of k numbers that sum up to n such that the following conditions are true:
Only numbers 1 through 9 are used.
Each number is used at most once.
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.

Example

Input: k = 3, n = 7
Output: [[1,2,4]]
Explanation:
1 + 2 + 4 = 7
There are no other valid combinations.

代码
class Solution {
    
    public List> combinationSum3(int k, int n) {
        List> ans = new ArrayList<>();
        dfs(ans, k, n, new ArrayList<>(), 1);
        return ans;
    }
    
    private void dfs(List> ans, int k, int n, List list, int begin) {
        if (n == 0 && list.size() != 0 && k == 0) {
            ans.add(new ArrayList(list));
            return;
        }
        
        if (n < 0 || begin > 9 || k < 0) {
            return;
        }
        
        for (int i = begin; i <= 9; i++) {
            list.add(i);
            dfs(ans, k - 1, n - i, list, i + 1);
            list.remove(list.size() - 1);
        }
        return;
    }
}
代码核心思路
  1. DFS;
  2. 保存中间结果(如函数dfs的第四个参数List list )
  3. 保存最终结果(如函数dfs的第一个参数List> ans)
  4. 恢复现场(list.remove(list.size() - 1))
总结

共同点:

  1. DFS
  2. 需要追踪中间结果
  3. 需要保存最终结果
  4. 需要恢复现场
  5. 根据需求在dfs函数内由for循环来找答案。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/873988.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号