leetCode练习(60)
2016-10-11 14:32
357 查看
题目:Permutation Sequence
难度:medium
问题描述:
The set
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
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的下一个排列。
具体代码如下:
方法二:不需要排序,可以直接计算出每位的值。例如n=3,k=2. 一共有3!=6中组合,最高位相同时有2!种可能。k=2时 k<=1*2!,因此最高位为1. 通过此思想,不管k为多少,都可以以同样的复杂度计算出来。具体代码如下:
难度: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):
"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.
解题思路:
在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 ; 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 ; int[] temp=new int ; 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; }
相关文章推荐
- Leetcode60: Binary Tree Level Order Traversal II
- 数据结构与算法 LeetCode编程练习--Delete Node in a Linked List
- Leetcode练习-Remove Duplicates from Sorted List
- LeetCode练习-翻转数字(Reverse Integer)
- C 练习实例51-60
- leetCode练习(68)
- leetCode练习(78)
- LeetCode Online Judge 题目C# 练习 - 4SUM
- LeetCode初级算法练习
- LeetCode Online Judge 题目C# 练习 - Decode Way
- LeetCode Online Judge 题目C# 练习 - Longest Substring Without Repeating Characters
- LeetCode Online Judge 题目C# 练习 - Minimum Path Sum
- leetCode练习(14)
- [编程练习][Two Sum]<LeetCode-1>
- leetCode练习(494)
- leetcode 58、Length of Last Word;59、Spiral Matrix II ;60、Permutation Sequence
- LeetCode Online Judge 题目C# 练习 - Roman to Integer
- leetCode练习(5)
- LeetCode Online Judge 题目C# 练习 - Two Sum
- leetCode练习(114)