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<Integer> 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<number.size();i++){
			System.out.print(number.get(i));
		}
	}
	
	public void swap(int index,int i,Vector<Integer> 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;
		}
	}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值