字典序全排列java实现

package permutation;

import java.util.Scanner;

public class DictionaryPermutation {
	
	// find the position need to exchange with minimum number which is larger than it
	public int getPosition(String str) {
		int pos = -1;
		char[] ch = str.toCharArray();
		for (int i = str.length() - 1; i > 0; --i) {
			if (ch[i] > ch[i - 1]) {
				pos = i - 1;
				return pos;
			}
		}
		return pos;
	}

	// judge current permutation whether it is last
	public boolean hasNextPermutation(int pos) {
		if (pos == -1)
			return false;
		return true;
	}
	
	// string reverse
	public String reverseString(String str) {
		char[] ch = str.toCharArray();
		for (int i = 0; i < str.length() / 2; ++i) {
			char temp = ch[i];
			ch[i] = ch[str.length() - 1 - i];
			ch[str.length() - 1 - i] = temp;
		}
		str = String.valueOf(ch);
		return str;
	}
	
	// get next permutation of current number
	public String nextPermutation(String str) {
		// TODO Auto-generated method stub
		char[] ch = str.toCharArray();
		int pos = getPosition(str);
		if (pos >= 0) {
			// exchange the ch[position] with the minimum number which is lager
			// than it in its right.
			for (int i = pos + 1; i < str.length(); ++i) {
				if (i == str.length() - 1 && ch[i] > ch[pos]) {
					char temp = ch[pos];
					ch[pos] = ch[i];
					ch[i] = temp;
				} else if (ch[pos] > ch[i]) {
					char temp = ch[pos];
					ch[pos] = ch[i - 1];
					ch[i - 1] = temp;
					break;
				}
			}
			String substr1 = new String();
			substr1 = String.valueOf(ch, pos + 1, str.length() - pos - 1);
			substr1 = reverseString(substr1);
			return String.valueOf(ch, 0, pos + 1) + substr1;
		}
		return null;
	}

	// print all permutation
	public int permutation(String str){
		char[] ch = str.toCharArray();
		for(int i = 0 ; i < str.length() ; ++i ){
			System.out.print(ch[i]+" ");
		}
		System.out.println();
		int times = 1;
		while (hasNextPermutation(getPosition(str))) {
			str = nextPermutation(str);
			ch = str.toCharArray();
			for(int i = 0 ; i < str.length() ; ++i ){
				System.out.print(ch[i]-48+" ");
			}
			System.out.println();
			++times;
		}
		System.out.println("times = " + times);
		return times;
	}
	
	//create
	public String creatPermutation(int number){
		char[] ch = new char[number];  
		for(int i = 1 ; i <= number;++i)
			ch[i-1] = (char) (i + 48);
		return String.valueOf(ch);
	}
	public static void main(String[] args) {
		DictionaryPermutation dp = new DictionaryPermutation();
		Scanner scan = new Scanner(System.in);
		System.out.println("please input the number of permutation:");
		int num = scan.nextInt();
		long start = System.currentTimeMillis();
		dp.permutation(dp.creatPermutation(num));
		long end = System.currentTimeMillis();
		System.out.println("run time: " + (double)Math.round(end-start)/1000+" second");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值