classSolution { public: string getPermutation(int n, int k){ vector<char> nums; for (int i = 1; i <= n; ++i) { nums.push_back('0' + i); } k -= 1; string result; for (int i = n; i > 0; --i) { int index = k / factorial(i - 1); k %= factorial(i - 1); result.push_back(nums[index]); nums.erase(nums.begin() + index); } return result; } private: intfactorial(int n){ int result = 1; for (int i = 2; i <= n; ++i) { result *= i; } return result; } };
classSolution { public String getPermutation(int n, int k) { List<Integer> nums = newArrayList<>(); for (inti=1; i <= n; ++i) { nums.add(i); } k -= 1; StringBuilderresult=newStringBuilder(); for (inti= n; i > 0; --i) { intindex= k / factorial(i - 1); k %= factorial(i - 1); result.append(nums.get(index)); nums.remove(index); } return result.toString(); } privateintfactorial(int n) { intresult=1; for (inti=2; i <= n; ++i) { result *= i; } return result; } }
结果
执行用时 : 1 ms, 击败 91.08% 使用 Java 的用户
内存消耗 : 40.16 MB, 击败 34.22% 使用 Java 的用户
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution(object): defgetPermutation(self, n, k): """ :type n: int :type k: int :rtype: str """ nums = [str(i) for i inrange(1, n + 1)] result = [] k -= 1 for i inrange(n, 0, -1): index, k = divmod(k, math.factorial(i - 1)) result.append(nums.pop(index)) return''.join(result)
结果
执行用时 : 13 ms, 击败 78.31% 使用 Python 的用户
内存消耗 : 11.48 MB, 击败 78.31% 使用 Python 的用户
Python3
1 2 3 4 5 6 7 8 9
classSolution: defgetPermutation(self, n: int, k: int) -> str: nums = [str(i) for i inrange(1, n + 1)] result = [] k -= 1# Convert k to index (0-based) for i inrange(n, 0, -1): index, k = divmod(k, math.factorial(i - 1)) result.append(nums.pop(index)) return''.join(result)
intfactorial(int n) { int result = 1; for (int i = 2; i <= n; ++i) { result *= i; } return result; } char* getPermutation(int n, int k) { char* result = (char*)malloc((n + 1) * sizeof(char)); result[n] = '\0'; char nums[n]; for (int i = 0; i < n; ++i) { nums[i] = '0' + i + 1; } k -= 1; for (int i = n; i > 0; --i) { int index = k / factorial(i - 1); k %= factorial(i - 1); result[n - i] = nums[index]; for (int j = index; j < i - 1; ++j) { nums[j] = nums[j + 1]; } } return result; }
publicclassSolution { publicstringGetPermutation(int n, int k) { char[] result = newchar[n]; char[] nums = newchar[n]; for (int i = 0; i < n; i++) { nums[i] = (char)('0' + i + 1); } k--; for (int i = n; i > 0; i--) { int index = k / Factorial(i - 1); k %= Factorial(i - 1); result[n - i] = nums[index]; Array.Copy(nums, index + 1, nums, index, i - index - 1); } returnnewstring(result); } privateintFactorial(int n) { int result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; } }
/** * @param {number} n * @param {number} k * @return {string} */ var getPermutation = function(n, k) { let result = []; let nums = []; for (let i = 1; i <= n; i++) { nums.push(i); } k--; for (let i = n; i > 0; i--) { let index = Math.floor(k / factorial(i - 1)); k %= factorial(i - 1); result.push(nums[index]); nums.splice(index, 1); } return result.join(''); }; functionfactorial(n) { let result = 1; for (let i = 2; i <= n; i++) { result *= i; } return result; }
classSolution { funcgetPermutation(_n: Int, _k: Int) -> String { if n ==1&& k ==1 { return"1" } var nums = [Int]() var remainingK = k for i in1...n { nums.append(i) } var result ="" for i instride(from: n, to: 1, by: -1) { let count = getCount(i) let count2 = count / i let rest = remainingK % count2 var index =0 if rest ==0 { index = remainingK / count2 -1 result.append("\(nums[index])") } else { index = (remainingK - rest) / count2 result.append("\(nums[index])") } nums.remove(at: index) remainingK -= count2 * index } iflet num = nums.first { result.append("\(num)") } return result } funcgetCount(_n: Int) -> Int { return n <2? n : n * getCount(n -1) } }
classSolution { fungetPermutation(n: Int, k: Int): String { if (n == 1 && k == 1) return"1" val nums = (1..n).toMutableList() var remainingK = k var result = "" for (i in n downTo 1) { val count = getCount(i) val count2 = count / i val rest = remainingK % count2 var index = 0 if (rest == 0) { index = remainingK / count2 - 1 result += nums[index].toString() } else { index = (remainingK - rest) / count2 result += nums[index].toString() } nums.removeAt(index) remainingK -= count2 * index } return result } privatefungetCount(n: Int): Int { returnif (n < 2) n else n * getCount(n - 1) } }
funcgetPermutation(n int, k int)string { if n == 1 && k == 1 { return"1" } nums := make([]int, n) for i := 0; i < n; i++ { nums[i] = i + 1 } var result string remainingK := k for i := n; i > 0; i-- { count := getCount(i) count2 := count / i rest := remainingK % count2 var index int if rest == 0 { index = remainingK/count2 - 1 result += strconv.Itoa(nums[index]) } else { index = (remainingK - rest) / count2 result += strconv.Itoa(nums[index]) } nums = append(nums[:index], nums[index+1:]...) remainingK -= count2 * index } return result } funcgetCount(n int)int { if n < 2 { return n } return n * getCount(n-1) }
# @param {Integer} n # @param {Integer} k # @return {String} defget_permutation(n, k) return'1'if n == 1 && k == 1 nums = (1..n).to_a result = '' remaining_k = k (n.downto(1)).each do |i| count = get_count(i) count2 = count / i rest = remaining_k % count2 index = 0 if rest.zero? index = remaining_k / count2 - 1 result += nums[index].to_s else index = (remaining_k - rest) / count2 result += nums[index].to_s end nums.delete_at(index) remaining_k -= count2 * index end result end defget_count(n) n < 2 ? n : n * get_count(n - 1) end
objectSolution{ defgetPermutation(n: Int, k: Int): String = { if (n == 1 && k == 1) return"1" var nums = (1 to n).toList var remainingK = k var result = "" for (i <- n to 1 by -1) { val count = getCount(i) val count2 = count / i val rest = remainingK % count2 var index = 0 if (rest == 0) { index = remainingK / count2 - 1 result += nums(index).toString } else { index = (remainingK - rest) / count2 result += nums(index).toString } nums = nums.patch(index, Nil, 1) remainingK -= count2 * index } result } defgetCount(n: Int): Int = { if (n < 2) n else n * getCount(n - 1) } }