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 ...
随机推荐
- jQuery :has() 选择器
实例 1.选取所有包含有 <span> 元素在其内的 <p> 元素: $("p:has(span)") 2. <!DOCTYPE html ...
- 基于线程池的多并发Socket程序的实现
Socket“服务器-客户端”模型的多线程并发实现效果的大体思路是:首先,在Server端建立“链接循环”,每一个链接都开启一个“线程”,使得每一个Client端都能通过已经建立好的线程来同时与Ser ...
- Jmeter(十三)Debug Sampler
上篇记录了关联的一些内容.当然,关联是有“风险”的,经常写的正则表达式,jsonpath,xpath自己认为是没问题的,可是就是跑起来有问题,百思不得其解,究竟是数据问题还是表达式的问题,本篇就来记录 ...
- [UE4]蓝图调试
1.蓝图下断点:选择蓝图节点按F9下断点:再按一下F9就会去掉断点. 2.游戏运行到断点会自动这暂停,鼠标移到某个变量上面,会显示该变量的值. 3.按F10执行下一步. 4.蓝图调试没有跳出函数的功能 ...
- [UE4]更新UI的三种方式
一.函数绑定 二.属性绑定 只会列出匹配的数据类型. 三.事件驱动更新 啦啦啦啦啦 结论:函数和属性绑定的原理都是每帧都去调用绑定的函数/属性,效率比较低下,一般不推荐使用.事件驱动更新的效率最好,性 ...
- log4j自带的两个类MDC和NDC作用以及用途
原文转载至: https://blog.csdn.net/joeyon/article/details/52982330 要想实现获取IP并显示在log中必须先了解log4j自带的两个类MDC和NDC ...
- 使用LiteOrm删除数据对象失败的坑
使用 LiteOrm.newSingleInstance(BaseApplication.getInstance(), Constant.DB_NAME); 在不同进程中创建了两次对象,在保存和删除的 ...
- 大数据Web可视化分析系统开发
下载地址 https://tomcat.apache.org/download-70.cgi 打开我们的idea 这些的话都可以按照自己的需求来修改 在这里新建包 新建一个java类 package ...
- CentOS7.4安装部署openstack [Liberty版] (二)
继上一篇博客CentOS7.4安装部署openstack [Liberty版] (一),本篇继续讲述后续部分的内容 一.添加块设备存储服务 1.服务简述: OpenStack块存储服务为实例提供块存储 ...
- oracle查看和替换含不可见字符(空白)
select lengthb('1397256'), dump('1397256') from dual; select ascii('') from dual; ), '') from dua ...