|
public class Permutation
{
private int[] a;
public Permutation(int[] a)
{
this.a = a;
}
public boolean isOk(int b,int e){//判断是否重复
if(b < e){
for(int i = b; i < e; i++){
if(a == a[e])
return false;
}
}
return true;
}
public void permutation(int k){
if(k >= a.length){
print();
}else{
for(int i = k; i < a.length; i++){
if(isOk(k,i)){
swap(i,k);
permutation( k+1 );
swap(i,k);
}
}
}
}
private void swap( int i, int k )
{
int temp = a;
a = a[k];
a[k] = temp;
}
private void print()
{
for( int i = 0; i < a.length; i++ )
{
System.out.print(a + " ");
}
System.out.println();
}
public static void main( String[] args )
{
Permutation p = new Permutation(new int[]{1,2,2,2});
p.permutation( 0 );
}
}
运行:
C:\poj>java Permutation
1 2 2 2
2 1 2 2
2 2 1 2
2 2 2 1 |
|