求排列组合的小技巧

求解组合数方法:

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值