leetCode练习(60)

题目:Permutation Sequence

难度:medium

问题描述:

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):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

解题思路:

在123456(n=6时)的所有组合中,找到第k小的数。想到了字典序,使用字典排序,从最小的123456开始,排k-1次即可得到第k小的数。

/********************最下方有更优解的更新****************************/

字典序如下 :(摘自百度百科)
设P是1~n的一个 全排列:p=p1p2......pn=p1p2......pj-1pjpj+1......pk-1pkpk+1......pn
1)从排列的右端开始,找出第一个比右边数字小的数字的序号j(j从左端开始计算),即 j=max{i|pi<pi+1}
2)在pj的右边的数字中,找出所有比pj大的数中最小的数字pk,即 k=max{i|pi>pj}(右边的数从右至左是递增的,因此k是所有大于pj的数字中序号最大者)
3)对换pj,pk
4)再将pj+1......pk-1pkpk+1......pn倒转得到排列p'=p1p2.....pj-1pjpn.....pk+1pkpk-1.....pj+1,这就是排列p的下一个排列。
具体代码如下:
public class m_60_PermutationSequence {
	public static String getPermutation(int n, int k) {
        int[] res=new int[n];
        String str="";
        for(int i=0;i<n;i++){
        	res[i]=i+1;
        }
        for(int i=1;i<k;i++){
        	nextorder(res);
        }
        for(int i=0;i<n;i++){
        	str+=res[i];
        }
        return str;
    }
	public static void nextorder(int[] res){
		int len=res.length;		
		int index=-1;
		int index2=-1;
		int t;
		for(int i=len-2;i>=0;i--){
			if(res[i]<res[i+1]){
				index=i;
				break;
			}
		}
		if(index!=-1){
			for(int i=index+1;i<len;i++){
				if(res[i]>res[index]){
					if(index2!=-1){
						index2=res[index2]>res[i]?i:index2;
					}else{
						index2=i;
					}
				}
			}
			t=res[index];
			res[index]=res[index2];
			res[index2]=t;
		}
		for(int i=0;i<(len-1-index)/2;i++){
			t=res[index+1+i];
			res[index+1+i]=res[len-1-i];
			res[len-1-i]=t;
		}
	}
	public static void main(String[]args){
		System.out.println(getPermutation(4,2));
	}
	
}
方法二:不需要排序,可以直接计算出每位的值。例如n=3,k=2.  一共有3!=6中组合,最高位相同时有2!种可能。k=2时 k<=1*2!,因此最高位为1. 通过此思想,不管k为多少,都可以以同样的复杂度计算出来。具体代码如下:
public static String getPermutation2(int n, int k){
		int[] res=new int[n];
		int[] temp=new int[n];
		String str="";
		for(int i=0;i<n;i++){
			temp[i]=i+1;
		}
		int jiecheng,ji;
		int count,j;
		for(int i=0;i<n;i++){
			jiecheng=1;
			for(j=n-1-i;j>=2;j--){
				jiecheng*=j;
			}
			System.out.println("jiecheng="+jiecheng);
			ji=(int)((k-0.5)/jiecheng)+1;//temp剩余的第ji个数
			System.out.println("k="+k+"ji="+ji);
			count=0;
			j=0;
			while(true){
				if(temp[j]!=-1){
					count++;
				}
				if(count==ji){
					res[i]=temp[j];
					temp[j]=-1;
					break;
				}
				j++;
			}
			k=(int)(k-0.5)%jiecheng+1;
		}
		for(int i=0;i<n;i++){
			str+=res[i];
		}
		return str;
	}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值