您的位置:首页 > 编程语言 > Java开发

字典序全排列java实现

2013-11-02 15:44 246 查看
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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: