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<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;}
}
代码核心思路
- DFS;
- 交换(如题目中的
swap
) - 保存中间结果(如函数dfs的第二个参数
List<Integer> list
) - 保存最终结果(如函数dfs的第一个参数
List<List<Integer>> ans
) - 恢复现场(
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;}
}
代码核心思路
- DFS;
- 保存中间结果(如函数dfs的第四个参数
List<Integer> list
) - 保存最终结果(如函数dfs的第一个参数
List<List<Integer>> ans
) - 恢复现场(
list.remove(list.size() - 1)
)
总结
共同点:
- DFS
- 需要追踪中间结果
- 需要保存最终结果
- 需要恢复现场
- 根据需求在
dfs函数内
由for循环
来找答案。
Permutation 和 Combination相关推荐
- 深入浅出统计学 第六章 排列与组合
内容简介 本章内容主要介绍了两个基本概念,排序与组合 其中组合是之后计算二项分布的预备知识 对于计算而言,重点在于理解其所适应的不同情况,并记忆公式. 两者区别(P261): 1. 排列与顺序有关 2 ...
- Python 案例001 (有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数)...
题目内容来自网络 ,加入了个人理解的过程 ,和点评 #!/usr/bin/python # -*- coding: UTF-8 -*-#Author : Wumi#题目:有四个数字:1.2.3.4,能 ...
- 排列公式和组合公式_排列与组合:排列公式与组合公式之间有什么区别?
排列公式和组合公式 Here's the short version. 这是简短的版本. Let's take ringing bells in a church as an example. 让我们 ...
- 数学词汇的英译,写文章,读文献必备
· 一般词汇 数学 mathematics, maths(BrE), math(AmE) 公理 axiom 定理 theorem 推论 corollary 计算 calculation 运算 ope ...
- Mathematics English Vocabulary (Cited)
一般词汇 数学 mathematics, maths(BrE), math(AmE) 公理 axiom 定理 theorem 计算 calculation 运算 operation 证明 prove ...
- [翻译] 数学翻译词汇
初等数学 elementary mathematics 高等数学 higher mathematics 现代数学 modern mathematics 基础代数 basic mathemat ...
- 排列组合_均匀分组和部分均匀分组的计算与示例/二项式定理二项式系数/求和中的对称性/莱布尼兹公式与推广的求导法则
文章目录 references 排列数/组合数公式 乘法原理和三大模型基础模型: 方幂模型 排列数Permutation 组合数combination 组合数的特点 证明 二项式定理中的含义(杨辉三角 ...
- 数学术语的英汉对照(权威,全面)
abbreviation 简写符号:简写 abscissa 横坐标 absolute complement 绝对补集 absolute error 绝对误差 absolute inequality 绝 ...
- 高等数学术语英汉对照
数学术语的英汉对照(权威,全面)English Chinese abbreviation 简写符号:简写 abscissa 横坐标 absolute complement 绝对补集 absolute ...
- 从n个不同元素中取出m个元素排列组合
01. 问题 问题01. 算法: 从n个不同元素中取出m个元素的排列数是多少? 这些排列分别是什么? (其中: n > 0; 0 < m ≤ n;) 问题02. 算法: 从n个不同元素中取 ...
最新文章
- Xamarin 学习笔记 - 配置环境(Windows iOS)
- 南方h5手簿使用说明书_雄脱使用非那雄胺米诺地尔效果
- 超强NLP思维导图,知识点全面覆盖:从基础概念到最佳模型,萌新成长必备资源...
- 不用SE11建Structure传Internal Table到Smartforms的方法
- javax.websocket.DeploymentException: The path [webScoketServiceBaidu/{appID}] is not valid.
- 程序员如何快速消除自己的知识短板?
- Nginx(五):动静分离
- 【Adobe Premiere Pro 2020】pr2020安装和基本操作【PR安装、新建项目流程、导入及管理素材项目文件、添加标记、创建出入点剪辑视频、快速剪接及自动音乐卡点的方法
- 【人工智能】重磅发布人工智能与机器学习全景式概览
- 快递查询—API接口
- 分治法的关键特征_算法系列之常用算法之一----分治算法
- linux malloc错误,如何规避GCC中“尝试使用中毒的malloc / calloc”错误?
- week7 PyCharm和Flask初应用
- 转贴: 人应该为自己而活
- 大型企业局域网安全解决方案
- 什么是SYN Flood?
- Flink/Hbase 异常 - 4.Sink 背压100% 与 hbase.util.RetryCounter.sleepUntilNextRetry 异常分析与排查
- 笔记: 计算天区面积的方法
- 元宇宙直播,不再是概念
- 20171001-构建之法:现代软件工程-阅读笔记2