Permutation Sequence
来源:互联网 发布:淘宝哪家iphone 编辑:程序博客网 时间:2024/06/05 08:04
The set [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.
public static String getPermutation(int n, int k) {// initialize all numbersArrayList<Integer> numberList = new ArrayList<Integer>();for (int i = 1; i <= n; i++) {numberList.add(i);}// change k to be indexk--;// set factorial of nint mod = 1;for (int i = 1; i <= n; i++) mod = mod * i;String result = "";// find sequencefor (int i = 0; i < n; i++) {mod = mod / (n - i);// find the right number(curIndex) ofint curIndex = k / mod;// update kk = k % mod;// get number according to curIndexresult += numberList.get(curIndex);// remove from listnumberList.remove(curIndex);}return result.toString();}
0 0
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- 程序员练级指南
- uva 10474 Where is the Marble?(简单题)
- 网络编程GUI:
- 愚人节的礼物
- 初识HTML
- Permutation Sequence
- UVA 573 The Snail
- 【第三次自考】第一阶段总结
- 基于递归的二叉树遍历
- 采药问题
- androd中主线程和非主线程的通信机制(Handler/Looper/Message)
- C++引用
- Vector
- eclipse完全支持jsp页面下HTML/JS/CSS智能提示