文章目录

  • 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<List<Integer>> permute(int[] nums) {List<List<Integer>> ans = new ArrayList<>();dfs(ans, new ArrayList<>(), 0, nums);return ans;}private void dfs(List<List<Integer>> ans, List<Integer> 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<Integer> list
  4. 保存最终结果(如函数dfs的第一个参数List<List<Integer>> 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<List<Integer>> combinationSum3(int k, int n) {List<List<Integer>> ans = new ArrayList<>();dfs(ans, k, n, new ArrayList<>(), 1);return ans;}private void dfs(List<List<Integer>> ans, int k, int n, List<Integer> list, int begin) {if (n == 0 && list.size() != 0 && k == 0) {ans.add(new ArrayList<Integer>(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<Integer> list
  3. 保存最终结果(如函数dfs的第一个参数List<List<Integer>> ans)
  4. 恢复现场(list.remove(list.size() - 1))

总结

共同点:

  1. DFS
  2. 需要追踪中间结果
  3. 需要保存最终结果
  4. 需要恢复现场
  5. 根据需求在dfs函数内for循环来找答案。

Permutation 和 Combination相关推荐

  1. 深入浅出统计学 第六章 排列与组合

    内容简介 本章内容主要介绍了两个基本概念,排序与组合 其中组合是之后计算二项分布的预备知识 对于计算而言,重点在于理解其所适应的不同情况,并记忆公式. 两者区别(P261): 1. 排列与顺序有关 2 ...

  2. Python 案例001 (有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数)...

    题目内容来自网络 ,加入了个人理解的过程 ,和点评 #!/usr/bin/python # -*- coding: UTF-8 -*-#Author : Wumi#题目:有四个数字:1.2.3.4,能 ...

  3. 排列公式和组合公式_排列与组合:排列公式与组合公式之间有什么区别?

    排列公式和组合公式 Here's the short version. 这是简短的版本. Let's take ringing bells in a church as an example. 让我们 ...

  4. 数学词汇的英译,写文章,读文献必备

    ·  一般词汇 数学 mathematics, maths(BrE), math(AmE) 公理 axiom 定理 theorem 推论 corollary 计算 calculation 运算 ope ...

  5. Mathematics English Vocabulary (Cited)

    一般词汇 数学 mathematics, maths(BrE), math(AmE) 公理 axiom 定理 theorem 计算 calculation 运算 operation 证明 prove ...

  6. [翻译] 数学翻译词汇

    初等数学   elementary mathematics 高等数学  higher mathematics 现代数学  modern mathematics 基础代数  basic mathemat ...

  7. 排列组合_均匀分组和部分均匀分组的计算与示例/二项式定理二项式系数/求和中的对称性/莱布尼兹公式与推广的求导法则

    文章目录 references 排列数/组合数公式 乘法原理和三大模型基础模型: 方幂模型 排列数Permutation 组合数combination 组合数的特点 证明 二项式定理中的含义(杨辉三角 ...

  8. 数学术语的英汉对照(权威,全面)

    abbreviation 简写符号:简写 abscissa 横坐标 absolute complement 绝对补集 absolute error 绝对误差 absolute inequality 绝 ...

  9. 高等数学术语英汉对照

    数学术语的英汉对照(权威,全面)English Chinese abbreviation 简写符号:简写 abscissa 横坐标 absolute complement 绝对补集 absolute ...

  10. 从n个不同元素中取出m个元素排列组合

    01. 问题 问题01. 算法: 从n个不同元素中取出m个元素的排列数是多少? 这些排列分别是什么? (其中: n > 0; 0 < m ≤ n;) 问题02. 算法: 从n个不同元素中取 ...

最新文章

  1. Xamarin 学习笔记 - 配置环境(Windows iOS)
  2. 南方h5手簿使用说明书_雄脱使用非那雄胺米诺地尔效果
  3. 超强NLP思维导图,知识点全面覆盖:从基础概念到最佳模型,萌新成长必备资源...
  4. 不用SE11建Structure传Internal Table到Smartforms的方法
  5. javax.websocket.DeploymentException: The path [webScoketServiceBaidu/{appID}] is not valid.
  6. 程序员如何快速消除自己的知识短板?
  7. Nginx(五):动静分离
  8. 【Adobe Premiere Pro 2020】pr2020安装和基本操作【PR安装、新建项目流程、导入及管理素材项目文件、添加标记、创建出入点剪辑视频、快速剪接及自动音乐卡点的方法
  9. 【人工智能】重磅发布人工智能与机器学习全景式概览
  10. 快递查询—API接口
  11. 分治法的关键特征_算法系列之常用算法之一----分治算法
  12. linux malloc错误,如何规避GCC中“尝试使用中毒的malloc / calloc”错误?
  13. week7 PyCharm和Flask初应用
  14. 转贴: 人应该为自己而活
  15. 大型企业局域网安全解决方案
  16. 什么是SYN Flood?
  17. Flink/Hbase 异常 - 4.Sink 背压100% 与 hbase.util.RetryCounter.sleepUntilNextRetry 异常分析与排查
  18. 笔记: 计算天区面积的方法
  19. 元宇宙直播,不再是概念
  20. 20171001-构建之法:现代软件工程-阅读笔记2

热门文章

  1. ERD Online 4.0.0新版本震撼来袭,超强国产免费在线数据建模系统
  2. 2023年最新谷歌Google帐号Gmail邮箱账号怎么注册成功的方法与教程?
  3. 【5G】5GC网元服务及对应消息
  4. Atom处理器喜迎周岁生日 主频达2GHz
  5. 关于量子领域的一些概念
  6. 医学成像学习笔记(一):核磁共振成像(MRI)k空间为何是图像频谱详解
  7. VML标记与通用属性
  8. 微信小程序防抖功能的实现
  9. 关键字: CCTV5 天下足球 盗版
  10. angularjs技术