Leetcode全排列问题Java版

1、编号30 Next Permutation
2、编号44 Permutations
3、编号45 Permutations II

4、编号60 Permutation Sequence


1、编号30 Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1


public void nextPermutation(Vector number){
		if(null == number || 1 >= number.size()){
			return;
		}
		else{
			int end = number.size()-1;
			for(int i = number.size()-2;i>=0;i--){
				if(number.get(i) < number.get(end)){
					do end--;
					while(number.get(i) <  number.get(end));
					swap(i ,end+1,number);
					break;
				}
				else{
					int temp = number.remove(i);
					number.add(temp);
				}
			}
		}
		for(int i = 0;i v){
		int temp = v.get(index);
		v.set(index, v.get(i));
		v.set(i, temp);
	}


2、编号44 Permutations

Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].

public int permutation(int[] collection,int start,int end){
		int total = 0;
		if(start == end) {
			for (int i = 0; i <= end; i++) { 
                System.out.print(collection[i]);  
            }
			System.out.println();
			return 1;
		}
		for(int i = start;i<=end;i++){
			swap(start,i,collection);
			total+=permutation(collection,start+1,end);
			swap(i,start,collection);
		}
		return total;
	}


3、编号45 Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].

public void permutateDup(int[] collection,int start,int end){
		if(start == end ||( (start == end-1) && (collection[start] == collection[end]))) {
			for (int i = 0; i <= end; i++) {  
                System.out.print(collection[i]);  
            }
			System.out.println();  
			return;
		}
		for(int i = start;i<=end;i++){
			if(collection[i] == collection[start] && i!=start)
				continue;
			swap(start,i,collection);
			permutateDup(collection,start+1,end);
			swap(i,start,collection);
		}
	}


4、编号60 Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.

public void permutateSequence(int[] set,int k){
		int totalCount = permutation(set,1,set.length-1);
		int a = (k-1)/totalCount;
		int b = (k-1)%totalCount+1;
		swap(a,0,set);
		int result = permutation(set,0,set.length-1,b,0);
		if(-1 != result){
			System.out.println("not found");
		}
	}
	
	public int permutation(int[] collection,int start,int end,int pos,int curPos){
		int temp = curPos;
		if(start == end ){
			curPos++;
			if(curPos == pos) {
				for (int j = 0; j <= end;j ++) {  
	                System.out.print(collection[j]);  
	            }
				System.out.println();
				return -1;
			}
			return 1;
		}
		else{
			for(int i = start;i<=end;i++){
				swap(i,start,collection);
				int result = permutation(collection,start+1,end,pos,curPos);
 				if(result == -1){
					return -1;
				}
				else{
					curPos+=result;
				}
				swap(start,i,collection);
			}
			return curPos-temp;
		}
	}




你可能感兴趣的:(算法)