2019.12.19 LeetCode 从零单刷个人笔记整理(持续更新)
github:https://github.com/ChopinXBP/LeetCode-Babel
之前有做过两道排列相关的题目:
LeetCode(46):全排列 Permutations(Java)
LeetCode(31):下一个排列 Next Permutation(Java)
但是,单纯依靠之前的全排列方法做出的排列是非字典序的,如果要应对本题,除非先求所有排列再进行排序,效率极低。本题有三种求第k个排列的高效方法:
1.剪枝回溯法
能够按序获得排列的递归回溯法。
2.按位定位法(数学)
每一位确定后,剩余排列数字的总数是可知的,因此可以从高到低确定每一位。
数字都是从1开始的连续自然数,排列出现的次序可以推算出来,对于n=4, k=15 找到k=15排列的过程:
1 + 对2,3,4的全排列 (3!个)
2 + 对1,3,4的全排列 (3!个) 3, 1 + 对2,4的全排列(2!个)
3 + 对1,2,4的全排列 (3!个)-------> 3, 2 + 对1,4的全排列(2!个)-------> 3, 2, 1 + 对4的全排列(1!个)-------> 3214
4 + 对1,2,3的全排列 (3!个) 3, 4 + 对1,2的全排列(2!个) 3, 2, 4 + 对1的全排列(1!个)
确定第一位:
k = 14(从0开始计数)
index = k / (n-1)! = 2, 说明第15个数的第一位是3
更新k
k = k - index*(n-1)! = 2
确定第二位:
k = 2
index = k / (n-2)! = 1, 说明第15个数的第二位是2
更新k
k = k - index*(n-2)! = 0
确定第三位:
k = 0
index = k / (n-3)! = 0, 说明第15个数的第三位是1
更新k
k = k - index*(n-3)! = 0
确定第四位:
k = 0
index = k / (n-4)! = 0, 说明第15个数的第四位是4
最终确定n=4时第15个数为3214
3.康托展开(数论)
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 for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。
按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:
"123"
"132"
"213"
"231"
"312"
"321"
给定 n 和 k,返回第 k 个排列。
示例 1:
输入: n = 3, k = 3
输出: "213"
示例 2:
输入: n = 4, k = 9
输出: "2314"
说明:
给定 n 的范围是 [1, 9]。
给定 k 的范围是[1, n!]。
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
*
* Given n and k, return the kth permutation sequence.
* 给定 n 和 k,返回第 k 个排列。
*
*/
public class PermutationSequence {
//剪枝回溯
public String getPermutation(int n, int k) {
ArrayList<String> result = new ArrayList<>();
Solution("", new boolean[n + 1], result, n, k);
return result.get(k - 1);
}
public void Solution(String temp, boolean[] isVisited, ArrayList<String> result, int n, int k){
if(temp.length() == n){
result.add(new String(temp));
}
for(int i = 1; i <= n; i++){
if(result.size() == k){
return;
}
if(isVisited[i]){
continue;
}
isVisited[i] = true;
String str = temp + i;
Solution(str, isVisited, result, n, k);
isVisited[i] = false;
}
}
//数学:每一位确定后,剩余排列数字的总数是可知的,因此可以从高到低确定每一位。
public String getPermutation2(int n, int k) {
StringBuilder sb = new StringBuilder();
// 候选数字
List<Integer> candidates = new ArrayList<>();
// 分母的阶乘数
int[] factorials = new int[n+1];
factorials[0] = 1;
int fact = 1;
for(int i = 1; i <= n; ++i) {
candidates.add(i);
fact *= i;
factorials[i] = fact;
}
k -= 1;
for(int i = n-1; i >= 0; --i) {
// 计算候选数字的index
int index = k / factorials[i];
sb.append(candidates.remove(index));
k -= index*factorials[i];
}
return sb.toString();
}
//康托展开
//https://baike.baidu.com/item/%E5%BA%B7%E6%89%98%E5%B1%95%E5%BC%80/7968428?fr=aladdin
public String getPermutation3(int n, int k) {
LinkedList<Character> nums = new LinkedList<>();
for(int i = 1; i <= 9; i++){
nums.add((char)(i + '0'));
}
ArrayList<Integer> newlist = new ArrayList<>();
int[] factor ={1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
StringBuilder result = new StringBuilder();
for(--k; n-- != 0; k %= factor[n]){
int i = k / factor[n];
result.append(nums.get(i));
nums.remove(i);
}
return result.toString();
}
}
#Coding一小时,Copying一秒钟。留个言点个赞呗,谢谢你#