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.
public class Solution { public String getPermutation(int n, int k) { int t = 1;; List<Integer> list = new ArrayList<Integer>(); for (int i = 1; i <= n; i++) { t *= i; list.add(i); } k--; StringBuilder sb = new StringBuilder(); for (int i = n; i >= 1; i--) { t = t/i; int index = k/t; sb.append(list.get(index)); list.remove(index); k = k % t; } return sb.toString(); } }
相关推荐
236 Permutation Sequence 573 237 Generate Parentheses 575 238 Combination Sum 577 239 Combination Sum II 579 240 Combination Sum III 581 241 Combinations 583 242 Letter Combinations of a Phone Number ...
Permutation Sequence Valid Sudoku Trapping Rain Water Rotate Image Plus One Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority Element Rotate Array Contains Duplicate Contains Duplicate II ...
Sequence 7 Two Sum Hash,夹逼均可 8 3Sum Hash法转换2sum 9 3Sum Closest Sort +夹逼法 10 4Sum Sort +夹逼法 11 Remove Element 12 Next Permutation 公式 13 Permutation Sequence 公式 14 Valid Sudoku 15 ...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for ...31 | [Next Permutation](https://leetcode.com/problems/next-permutation/)| ...
An image encryption scheme based on constructing large permutation with chaotic sequence
Adaptive Hybrid Algorithms for the Sequence-Dependent Setup Time Permutation Flow Shop Scheduling Problem
Output: A permutation (reordering) ha0 1; a0 2; : : : ; a0 ni of the input sequence such that a0 1 a0 2 a0 n. For example, given the input sequence h31; 41; 59; 26; 41; 58i, a sorting algorithm ...
An Invasive Weed Optimization (IWO) scheduling algorithm for solving Permutation Flow-shop Scheduling Problem (PFSP) is proposed. The Most Position Value (MPV) method is used to coding the weed ...
提出RMSP(Random Matrix Sequence Permutation)方法,同时完成帧内宏块(MacroBlock,MB )之间、块内VLC(Variable Length Coding)码字之间双重互补的乱序加密,并利用随机序列生成随机乱序密钥矩阵序列,供每帧和每块...
A partition of a string A is a sequence P = (P1, P2, . . . , Pm) of strings, called the blocks, whose concatenation is equal to A. Given a partition P of a string A and a partition Q of a string B, we...
Firstly, a simple approach is put forward to calculate the makespan of job sequence. Secondly, the most position value (MPV) method is used to code the weed individuals so that fitness values can be ...
Permutation Inversion of Array (Using Merge Sort) Stock Buy and Sell Rotate Matrix 第3天:(数学) Excel 列号 在 log N 中查找 n^x 在数字的阶乘中计算尾随零 在 Log N 网格中查找 GCD 唯一路径通过 GFG 中的...
第4章 序列式容器(sequence containers) 113 4.1 容器概观与分类 113 4.1.1 序列式容器(sequence containers) 114 4.2 vector 115 4.2.1 vector 概述 115 4.2.2 vector 定义摘要 115 4.2.3 vector 的迭代...
4.1.1 序列式容器(sequence containers) 114 4.2 vector 115 4.2.1 vector 概述 115 4.2.2 vector 定义摘要 115 4.2.3 vector 的迭代器 117 4.2.4 vector 的数据结构 118 4.2.5 vector 的构造与内存管理:...