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 ...
随机推荐
- eKingCloud 从 OpenStack 到 OpenInfra 演进之路
本内容首发于 2016/06/21 北京 OpenInfra 大会上本人的演讲 发文章要求至少150个字,那就把最后一页说明一下吧. 我前面介绍了我们的5大产品,包括企业的私有云架构和实践,包括企业数 ...
- centos6.5最小化安装之后装图形化界面
查看自己系统环境 # cat /etc/issue 先要安装KDE桌面环境,执行命令: # yum groupinstall "X Window System" "KDE ...
- 01-配置java开发环境
JDK 1.8软件下载地址 (Oracle公司的官方网站) http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads ...
- 基于HAProxy+Keepalived高可用负载均衡web服务的搭建
一 原理简介 1.HAProxyHAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web ...
- Linux下单独编译安装PHP扩展包
首先进入PHP源码目录,比如这个: root@vultr:~/php-/ext/soap# 运行下PHP目录下的phpize,接着就可以和普通代码一样,配置,编译,安装了(注意:有些库可能可以配置参数 ...
- sublime 打开import require 模块文件的url 或路径的插件
结果一番周折,终于发现sublime提供的一个插件(open url)可以实现打开import file 路径的文件,比如import demo from “../path” 的文件在新窗口或者新标签 ...
- Java - 32 Java 多线程编程
Java 多线程编程 Java给多线程编程提供了内置的支持.一个多线程程序包含两个或多个能并发运行的部分.程序的每一部分都称作一个线程,并且每个线程定义了一个独立的执行路径. 多线程是多任务的一种特别 ...
- Hdu 4622 Reincarnation(后缀自动机)
/* 字符串长度较小, 可以离线或者直接与处理所有区间的答案 动态加入点的时候, 因为对于其他点的parent构造要么没有影响, 要么就是在两个节点之间塞入一个点, 对于minmax的贡献没有改变 所 ...
- Java分布式锁的三种实现方案(redis)
方案一:数据库乐观锁 乐观锁通常实现基于数据版本(version)的记录机制实现的,比如有一张红包表(t_bonus),有一个字段(left_count)记录礼物的剩余个数,用户每领取一个奖品,对应的 ...
- Java中同步的几种实现方式
1.使用synchronized关键字修饰类或者代码块: 2.使用Volatile关键字修饰变量: 3.在类中加入重入锁. 代码示例: 非同步状态下: public static void main( ...