Leetcode60 Permutation Sequence (string)
来源:互联网 发布:2017程序员笔记本推荐 编辑:程序博客网 时间:2024/06/06 00:13
题意:给一个n,一个k,求1到n的所有数字的全排列按从小到大顺序排列的第k大的数
分析:可以参考康托编码2333,不过自己想也想得明白,比如说n=5的话,求第一个数字时每个数都有4!组子数,也就是/4!也就得到了开头的那个数字,然后数字是不能重复的,依次类推就能求出所有的数,注意整除时那里得处理下,也就是一些细节问题就行
AC:
class Solution {public: string getPermutation(int n, int k) { string s(n,'0'); string ans; int f=1; int index; for(int i=1;i<=n;i++) { s[i-1]=i; f*=i; }//s:1,2,3,4,5 while(n) { f/=n;//不断求(n-1)! index=k/f; if(k%f==0) index--; ans+=s[index]+'0'; s.erase(s.begin()+index); k-=index*f; n--; } return ans; }};
阅读全文
0 0
- Leetcode60 Permutation Sequence (string)
- LeetCode60:Permutation Sequence
- Leetcode60 Permutation Sequence
- LeetCode60. Permutation Sequence
- leetcode60. Permutation Sequence
- leetcode60~Permutation Sequence
- LEETCODE60. Permutation Sequence
- LeetCode60——Permutation Sequence
- leetcode60-Permutation Sequence(求指定位置的排列)
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- CSS中line-height和height的区别
- lotus中常用的公式
- Django REST framework-API指南03-Class-based Views 原创翻译
- 实现Web项目多个页面之间互相通信(web多屏通信)
- nginx
- Leetcode60 Permutation Sequence (string)
- grep命令
- 类加载机制
- Linux Shell编程入门
- Codeforces 230E Triangles【思维】
- 基于 HED网络TensorFlow 和 OpenCV 实现图片边缘检测
- openstack
- Python 25行代码实现图片转字符画
- Docker中安装OpenvSwitch 附rpm文件下载