60. Permutation Sequence
2016-03-22 10:27
369 查看
The set
unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
Subscribe to see which companies asked this question
第一个数决定在第几个[0,(n-1)!],[(n-1)!,2(n-1)!]……这样的区间内,第一个确定之后确定第二个……以此类推
[1,2,3,…,n]contains a total of n!
unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
Subscribe to see which companies asked this question
第一个数决定在第几个[0,(n-1)!],[(n-1)!,2(n-1)!]……这样的区间内,第一个确定之后确定第二个……以此类推
public class Solution { public String getPermutation(int n, int k) { String ret = ""; int s = 1; for (int i = n-1;i>1;i--){ s*=i; } List <Integer> ku = new LinkedList <>(); for(int i = 1;i<=n;i++){ ku.add(i); } k--; for(int i = 1;i<=n;i++){ int tem = k/s; k = k%s; if(n-i!=0)s = s/(n-i); ret+=""+ku.get(tem); ku.remove(tem); } return ret; } }
相关文章推荐
- 手机GUI自动化测试介绍 | 有道技术沙龙博客
- C#中怎么生成36位的GUID
- UIViewContentMode各类型效果
- iOS开发~UITableViewCell重用机制总结
- android UI进阶之实现listview的分页加载
- libraries for bitcode must be generated from Xcode Archive or Install build forARM7 解决办法
- DelayQueue 队列
- 《iOS Human Interface Guidelines》——Contact Add Button
- UIStepper
- SQL Interview Questions
- PHP基础之如何调试PHP程序(HBuilder)
- pywinauto首页、文档和下载 - GUI 自动化测试框架 - 开源中国社区
- 原生的强大DOM选择器querySelector
- Android UI自动化测试最佳实践 - 51Testing软件测试网-中国软件测试人的精神家园
- [leetcode] 308. Range Sum Query 2D - Mutable 解题报告
- request.getParameter() 和request.getAttribute() 区别
- UIView之setNeedsDisplay与drawRect 和 setNeedsLayout与layoutSubViews
- Integer Inquiry(大数)
- SOAPUI 接口测试学习笔记节选 结果写入操作方法介绍
- UGUI UI操作事件