LeetCode 题解 Permutation Sequence 需要优化!
题目大意:给出n和k,找到1..n这些数组成的有序全排列中的第k个。
首先,n的全排列可以分成n组,每一组由n-1个数组成。
例如 3的全排列,分成三组:
1 2 3 和 1 3 2
2 1 3 和 2 3 1
3 1 2 和 3 2 1
每一组的个数是(n-1)!,每一组的打头的都是i 。第i组以i打头。
先求 x = (k-1) / (n-1)! + 1
比如 n = 3, k = 4.
x = 3/2 + 1 = 2,得到 第四个实际上第二组里面。
y = (k-1)%(n-1)! + 1 = 2,即,要求的东西在第二组的第二个数。
第二组的特征是,以2开头。所以我们找到第2个数,放到string ans的最高位置。
然后,求剩下的数里面的全排列的第y个。 即把2去掉,用1 和 3做全排列,取其中的第2个。
这时候,回到初始的步骤。递归即可。
垃圾的代码,现在脑子懵。。写的代码跟屎一样,完全没有逻辑啊!要优化!!
- class Solution {
- public:
- int factorial(int n) //计算n的阶乘
- {
- if(n == || n == ) return ;
- return n*factorial(n-);
- }
- void back_track(vector<int>&used, string &ans, int n, int cnt, int k) //cnt表示ans里有几个元素。找到第k个。
- {
- if(cnt == n) return; //所有的都用完了
- int fac = factorial(n--cnt); //初始是n-1
- int x = (k-)/fac + ; //在第x 个序列 第一个数是第几个
- k = (k-)%fac + ; //x序列的第k个
- int i,j;
- i = ;
- while(used[i]) i ++; //找到第一个没用过的元素
- for(j = ; j < x;i++) //这时候x指向的是第1个未使用的元素
- {
- if(!used[i]) j++; //此时x走过了j-1个元素。
- }
- while(used[i]) i++; //找到第x个未使用的元素
- ans.append(,i + ''); //把这个元素放在头
- used[i] = true; //这个元素被使用了
- back_track(used, ans, n, cnt + ,k); //用剩下的去构造第k个元素
- }
- string getPermutation(int n, int k) {
- int i;
- vector<int> used(n+,false);
- string ans;
- back_track(used,ans,n,,k);
- return ans;
- }
- };
LeetCode 题解 Permutation Sequence 需要优化!的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode 题解]: Permutation Sequcence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] 60. Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【leetcode】 Permutation Sequence (middle)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- leetcode 60. Permutation Sequence(康托展开)
描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- leetcode 之 Permutation Sequence
Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- 【Leetcode】Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- Java NIO系列教程(十一) Pipe
Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 这里是Pipe原理的图示: 创建管道 通过Pi ...
- C# int.ToString()
C# int.ToString() 格式化数值:有时,我们可能需要将数值以一定的格式来呈现,就需要对数值进行格式化.我们使用格式字符串指定格式.格式字符串采用以下形式:Axx,其中 A 为格式说明符, ...
- OpenStack存储(单节点)
一.OpenStack Swift对象存储 1.安装Swift服务 在controller节点依次执行iaas-install-swift-controller.sh和iaas-install-swi ...
- Jmeter(三十二)Jmeter Question 之 “自定义函数开发”
“技术是业务的支撑”,已经不是第一次听到这句话,因为有各种各样的需求,因此衍生了许多各种各样的技术.共勉! 前面有提到提到过Jmeter的安装目录结构,也提到Jmeter的常用函数功能,有部分工作使用 ...
- Jmeter(二十)Beanshell or JSR223
有关Beanshell和JSR223组件的部分,早就想写一大篇幅随笔进行记录,苦于不知如何去描述这两部分的内容,一直在修改随笔. 介绍一下Beanshell: Beanshell是轻量级Java,支持 ...
- 获取器操作都是针对数据而不是数据集的,要通过append()方法添加数据表不存在的字段
获取器操作都是针对数据而不是数据集的,要通过append()方法添加数据表不存在的字段 public function getMembership(){ //加入会员s_id = 1 $busines ...
- 集群Redis使用 Python pipline大批量插入数据
class myRedis(object): def __init__(self,redis_type=None,**args): if redis_type == " ...
- python文件相关
文件操作基本流程初探 f = open('chenli.txt') #打开文件 first_line = f.readline() print('first line:',first_line) #读 ...
- php添加多组数据到数据库
//添加sql的数据 $sqldatas=getParam('sqldatas');//这里的sqldatas是从前台传过来的json字符串 //将json字符串转为json对象 $sqldata=j ...
- 2-scala集合
1.集合的数据结构: array.seq .set .map(映射) 2.集合分类 集合分为可变集合和不可变集合(除元组外,元组没有可变和不可变) scala.collection.immutable ...