leetcode 60: Permutation Sequence
2015-08-02 14:15
447 查看
class Solution { public: string getPermutation(int n, int k) { string res; int divider[10];/ 4000 /save 1! 2! ... (n-1)! bool visited[10]={0};//mark the numbers are already used divider[1]=1; for(int i=2;i<=n;i++) divider[i]=divider[i-1]*i; k--;//this is for the calculation of every number for(int i=1;i<=n;i++) { int count=k/divider[n-i]+1; int j; for(j=1;j<=n;j++) { if(!visited[j]) count--; if(!count) break; } visited[j]=1; res+=j+'0'; k%=divider[n-i]; } return res; } };
相关文章推荐
- Common Subsequence(最长公共子序列DP)
- 调整UILabel行高间距
- codeforces 566F F. Clique in the Divisibility Graph(dp)
- 常用类库StringBuilder
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
- 在iOS 8中使用UIAlertController
- 【转】UITableView使用详解
- 洛谷1373 小a和uim之大逃离
- easyui dataGrid接收嵌套JSON
- easyui dialog子窗口关闭父窗口
- android:layout_centerHorizontal="true"和 android:gravity="center_horizontal"什么区别?
- Selenium2学习-026-WebUI自动化实战实例-024-获取页面元素
- uuid (guid)全局唯一标识符
- Android SDK Tools Platform-tools Build-tools
- hdu 5329 Question for the Leader 枚举,划分图为k个联通的子图
- Gym 100570E : Palindrome Query
- IOS开发之UIScrollViewDelegate详解
- iOS 人机交互设计(开发)指南及一些综合知识整合
- Codeforces 558 D. Guess Your Way Out! II
- Selenium2学习-025-WebUI自动化实战实例-023-页面快照截图应用之一 -- 常规截图(全页面)