组合和全排列

package charSort;

import java.util.ArrayList;
import java.util.List;

/**
 * 几位的组合
 * @author root
 *
 */
public class CharSort {

	static String ss = "abc";
	static char[] st = new char[10];
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		sort(ss, 0, 0);
	}
	//所有2位的组合
	public static void sort(String ss, int start, int depth){
		if(depth>1){
			st[start]='\0';
			System.out.println(st);
			return;
		}
		for(int i=start; i<ss.length(); i++){
			st[depth] = ss.charAt(i);
			sort(ss, i+1, depth+1);
		}
	}
}

ab

ac

bc

package dictionary_order;
/**
 * 全排列
 * @author root
 *
 */
public class FullPermutation {

	static String s = "abc";
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		change(0, s.toCharArray());
	}

	public static void change(int k, char []data){
		if(k==data.length-1){
			print(data);
			return;
		}
		char temp;
		for(int i = k;i<data.length ;i++){
			temp = data[k];
			data[k] = data[i];
			data[i] = temp;
			change(k+1, data);
			temp = data[k];
			data[k] = data[i];
			data[i] = temp;
		}
	}
	
	public static void print(char[] data){
		for(int i=0; i<data.length; i++){
			System.out.print(data[i]);
		}
		System.out.println();
	}
	
}


bac
bca
cba
cab

posted on 2016-03-05 13:55  长456风  阅读(219)  评论(0编辑  收藏  举报

导航