求排列组合的小技巧
来源:互联网 发布:php能做爬虫吗 编辑:程序博客网 时间:2024/06/04 23:21
求解组合数方法:
public class Combination {public List<List<Integer>> combination(int[] a,int m){ dfs(-1,0,m,a); return re; }List<List<Integer>> re=new ArrayList<List<Integer>>();List<Integer> trace=new ArrayList<Integer>();public void dfs(int k,int level,int m,int[] a){if(level==m){re.add(new ArrayList<Integer>(trace));return;}for(int i=k+1;i<a.length;i++){trace.add(a[i]);dfs(i,level+1,m,a);trace.remove(trace.size()-1);}}public static void main(String[] args) {Combination c=new Combination();System.out.println(c.combination(new int[]{1,2,3,4},2));}}
组合数去重两种做法:
做法1:
public class Combination2 {//去重public List<List<Integer>> combination(int[] a,int m){ Arrays.sort(a);//组合数的去重一定要先排序 dfs(-1,0,m,a); return re;}List<List<Integer>> re=new ArrayList<List<Integer>>();List<Integer> trace=new ArrayList<Integer>();public void dfs(int k,int level,int m,int[] a){if(level==m){re.add(new ArrayList<Integer>(trace));return;}HashSet<Integer> set=new HashSet<Integer>();for(int i=k+1;i<a.length;i++){if(set.contains(a[i])) continue;set.add(a[i]);trace.add(a[i]);dfs(i,level+1,m,a);trace.remove(trace.size()-1);}}public static void main(String[] args) {Combination2 c=new Combination2();System.out.println(c.combination(new int[]{1,2,1,4},2));}}
做法2:
public class Combination3 {//去重public List<List<Integer>> combination(int[] a,int m){ Arrays.sort(a);//组合数的去重一定要先排序 dfs(-1,0,m,a); return re;}List<List<Integer>> re=new ArrayList<List<Integer>>();List<Integer> trace=new ArrayList<Integer>();public void dfs(int k,int level,int m,int[] a){if(level==m){re.add(new ArrayList<Integer>(trace));return;}for(int i=k+1;i<a.length;i++){if(i!=k+1&&a[i]==a[i-1]) continue;trace.add(a[i]);dfs(i,level+1,m,a);trace.remove(trace.size()-1);}}public static void main(String[] args) {Combination3 c=new Combination3();System.out.println(c.combination(new int[]{1,2,1,4},2));}}求解排列数:
两种做法:
1 通过swap的方式
2 通过boolean flag[] 数组标记已取得 数组的方法
方法1:
public class Permutation {public List<List<Integer>> permutatation(int[] a,int m){if(a.length==0) return re;dfs(0,a,m);return re;}List<List<Integer>> re=new ArrayList<List<Integer>>();public void dfs(int k,int[] a,int m){if(k==m){List<Integer> temp=new ArrayList<Integer>();for(int i=0;i<m;i++) temp.add(a[i]);re.add(temp);return;}for(int i=k;i<a.length;i++){swap(a,k,i);dfs(k+1,a,m);swap(a,k,i);}}private void swap(int[] a, int i, int j) {int temp=a[i]; a[i]=a[j]; a[j]=temp;}public static void main(String[] args) {Permutation s=new Permutation();System.out.println(s.permutatation(new int[]{1,2,3,4},2));}}针对这种方法的去重:
public class Permutation2 {public List<List<Integer>> permutatation(int[] a,int m){if(a.length==0) return re;dfs(0,a,m);return re;}List<List<Integer>> re=new ArrayList<List<Integer>>();public void dfs(int k,int[] a,int m){if(k==m){List<Integer> temp=new ArrayList<Integer>();for(int i=0;i<m;i++) temp.add(a[i]);re.add(temp);return;}for(int i=k;i<a.length;i++){if(!isSwap(k,i,a)) continue;swap(a,k,i);dfs(k+1,a,m);swap(a,k,i);}}public boolean isSwap(int start,int end,int[] a){for(int i=start;i<end;i++){if(a[i]==a[end]) return false;}return true;}private void swap(int[] a, int i, int j) {int temp=a[i]; a[i]=a[j]; a[j]=temp;}public static void main(String[] args) {Permutation2 s=new Permutation2();System.out.println(s.permutatation(new int[]{1,1,1,4},2));}}
方法二:
public class Permutation3 {public List<List<Integer>> permutatation(int[] a,int m){if(a.length==0) return re;flag=new boolean[a.length];dfs(0,a,m);return re;}List<List<Integer>> re=new ArrayList<List<Integer>>();List<Integer> trace=new ArrayList<Integer>();boolean[] flag;public void dfs(int k,int[] a,int m){if(k>m) return;if(k==m){re.add(new ArrayList<Integer>(trace));return;}for(int i=0;i<a.length;i++){if(flag[i]) continue;trace.add(a[i]);flag[i]=true;dfs(k+1,a,m);flag[i]=false;trace.remove(trace.size()-1);}}public static void main(String[] args) {Permutation3 s=new Permutation3();System.out.println(s.permutatation(new int[]{1,2,3,4},2));}}
方法二的去重:
去重1:
public class Permutation4 {//去重public List<List<Integer>> permutatation(int[] a,int m){if(a.length==0) return re;flag=new boolean[a.length];dfs(0,a,m);return re;}List<List<Integer>> re=new ArrayList<List<Integer>>();List<Integer> trace=new ArrayList<Integer>();boolean[] flag;public void dfs(int k,int[] a,int m){if(k>m) return;if(k==m){re.add(new ArrayList<Integer>(trace));return;}HashSet<Integer> set=new HashSet<Integer>();for(int i=0;i<a.length;i++){if(flag[i]) continue;if(set.contains(a[i])) continue;set.add(a[i]);trace.add(a[i]);flag[i]=true;dfs(k+1,a,m);flag[i]=false;trace.remove(trace.size()-1);}}public static void main(String[] args) {Permutation4 s=new Permutation4();System.out.println(s.permutatation(new int[]{1,2,1,4},2));}}
去重2:
public class Permutation5 {//去重public List<List<Integer>> permutatation(int[] a,int m){if(a.length==0) return re;Arrays.sort(a);flag=new boolean[a.length];dfs(0,a,m);return re;}List<List<Integer>> re=new ArrayList<List<Integer>>();List<Integer> trace=new ArrayList<Integer>();boolean[] flag;public void dfs(int k,int[] a,int m){if(k>m) return;if(k==m){re.add(new ArrayList<Integer>(trace));return;}for(int i=0;i<a.length;i++){if(flag[i]) continue;if(i!=0&&!flag[i-1]&&a[i]==a[i-1]) continue;trace.add(a[i]);flag[i]=true;dfs(k+1,a,m);flag[i]=false;trace.remove(trace.size()-1);}}public static void main(String[] args) {Permutation5 s=new Permutation5();System.out.println(s.permutatation(new int[]{1,2,1,4},2));}}
阅读全文
1 0
- 求排列组合的小技巧
- 排列组合的小程序
- 求一组整数的排列组合
- 求局部极值的小技巧
- D的小L排列组合
- 求排列组合
- 求1~9的的所有排列组合
- 求一个数组元素的所有排列组合
- 【求一个数组元素的所有排列组合】
- 排列组合 求一个字符串的排列
- 求数组的所有排列组合数
- 求几个数的全排列组合
- 五十道编程小题目 --- 43 求0—7所能组成的奇数个数(排列组合) java
- 常用的JS小技巧------求数组最大值
- ios 数组求和,求平均值的小技巧
- C#递归求排列组合
- C#求排列组合数
- 求排列组合算法
- Linux SSH管理用户登录
- Tetris tutorial in C++ platform independent focused in game logic for beginners
- C语言结构体,位段
- JS语言核心——词法结构
- vim编辑文件时当光标出现在注释行后按回车键怎么可以做到不现新注释行?
- 求排列组合的小技巧
- 设计模式--装饰者模式
- Qt 中关于 QWidget 添加菜单栏 菜单栏可以 随窗口大小伸缩
- Animation 动画类型
- IDEA的安装破解教程和使用说明(二)
- Golang 优化之路——HTTP长连接
- Android 计时器
- AppScan-Authentication Bypass Using HTTP Verb Tampering
- C++程序员学Java系列之八:随机数类Random