LeetCode 题解(169): Permutation Sequence
来源:互联网 发布:卸载ubuntu系统 编辑:程序博客网 时间:2024/04/29 11:58
题目:
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.
题解:从高位起,第i个数数应为 [1,...9]中第k / factoria[n-i]个未用的数, 而最后一位数(个位)应为最后一个未用的数。k的递推公式为:
k = k % factoria[n - i]
if k == 0:
k = factoria[n - i]。
C++版:
class Solution {public: string getPermutation(int n, int k) { if(n == 1) return "1"; vector<int> factoria(n-1, 0); factoria[0] = 1; for(int i = 1; i < n - 1; i++) factoria[i] = factoria[i-1] * (i + 1); vector<bool> used(n, false); int j = n - 2; string result; while(j >= 0) { int ith = ceil((double)k / factoria[j]); int i; for(i = 0; i < n; i++) { if(!used[i]) ith--; if(ith == 0) { used[i] = true; break; } } result += to_string(i + 1); k %= factoria[j]; if(k == 0) k = factoria[j]; j--; } for(int i = 0; i < n; i++) if(!used[i]) result += to_string(i + 1); return result; }};
Java版:
public class Solution { public String getPermutation(int n, int k) { if(n == 1) return "1"; StringBuffer result = new StringBuffer(); int[] factoria = new int[n-1]; boolean[] used = new boolean[n]; factoria[0] = 1; for(int i = 1; i < n - 1; i++) factoria[i] = factoria[i-1] * (i + 1); int j = n - 2; while(j >= 0) { int ith = (int)Math.ceil((double)k / factoria[j]); int i; for(i = 0; i < n; i++) { if(!used[i]) ith--; if(ith == 0) { used[i] = true; break; } } result.append(Integer.toString(i + 1)); k %= factoria[j]; if(k == 0) k = factoria[j]; j--; } for(int i = 0; i < n; i++) if(!used[i]) result.append(Integer.toString(i + 1)); return result.toString(); }}
Python版:
import mathclass Solution: # @param {integer} n # @param {integer} k # @return {string} def getPermutation(self, n, k): if n == 1: return "1" used = [False for i in range(n)] factoria = [1 for i in range(n-1)] for i in range(1, n-1): factoria[i] = factoria[i - 1] * (i + 1) result = "" j = n - 2 while j >= 0: ith = int(math.ceil(float(k) / factoria[j])) index = 0 for i in range(0, n): if not used[i]: ith -= 1 if ith == 0: used[i] = True index = i break result += str(index + 1) k %= factoria[j] if k == 0: k = factoria[j] j -= 1 for i in range(0, n): if not used[i]: result += str(i + 1) return result
0 0
- LeetCode 题解(169): Permutation Sequence
- LeetCode: Permutation Sequence
- LeetCode Permutation Sequence
- [Leetcode] Permutation Sequence
- [LeetCode] Permutation Sequence
- leetcode 74: Permutation Sequence
- leetcode Permutation Sequence
- [leetcode] Permutation Sequence
- [LeetCode]Permutation Sequence
- LeetCode:Permutation Sequence
- LeetCode-Permutation SequencePermutation Sequence
- [leetcode] Permutation Sequence
- LeetCode - Permutation Sequence
- LeetCode:Permutation Sequence
- LeetCode 60: Permutation Sequence
- Leetcode: Permutation Sequence
- leetcode Permutation Sequence
- LeetCode | Permutation Sequence
- 百度MP3 API
- java GUI(实例小项目--列出磁盘目录)
- 莫队算法模板
- 使用、学习 Atom编辑器 小记
- 1067. Sort with Swap(0,*) (25)
- LeetCode 题解(169): Permutation Sequence
- BZOJ 题目1503: [NOI2004]郁闷的出纳员(SBT+延迟操作)
- JAVA基础——常见数据结构简要
- Swift入门(九)——String与Int、Double、Float等数字相互转换
- 剑指Offer读书笔记(持续更新中)
- java GUI (菜单设计)
- protuse8安装破解手记
- LINUX修改path环境变量
- java GUI(实例项目--记事本)