Permutation
介绍
实现联合类型的全排列,将联合类型转换成所有可能的全排列数组的联合类型。 例如
ts
type perm = Permutation<'A' | 'B' | 'C'>;// ['A', 'B', 'C'] | ['A', 'C', 'B'] | ['B', 'A', 'C'] | ['B', 'C', 'A'] | ['C', 'A', 'B'] | ['C', 'B', 'A']
View on GitHubts
type perm = Permutation<'A' | 'B' | 'C'>;// ['A', 'B', 'C'] | ['A', 'C', 'B'] | ['B', 'A', 'C'] | ['B', 'C', 'A'] | ['C', 'A', 'B'] | ['C', 'B', 'A']
起点
ts
/* _____________ Your Code Here _____________ */typePermutation <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Permutation <'A'>, ['A']>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Permutation <'A' | 'B' | 'C'>, ['A', 'B', 'C'] | ['A', 'C', 'B'] | ['B', 'A', 'C'] | ['B', 'C', 'A'] | ['C', 'A', 'B'] | ['C', 'B', 'A']>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Permutation <'B' | 'A' | 'C'>, ['A', 'B', 'C'] | ['A', 'C', 'B'] | ['B', 'A', 'C'] | ['B', 'C', 'A'] | ['C', 'A', 'B'] | ['C', 'B', 'A']>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Permutation <boolean>, [false, true] | [true, false]>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Permutation <never>, []>>,]
take the challengets
/* _____________ Your Code Here _____________ */typePermutation <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Permutation <'A'>, ['A']>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Permutation <'A' | 'B' | 'C'>, ['A', 'B', 'C'] | ['A', 'C', 'B'] | ['B', 'A', 'C'] | ['B', 'C', 'A'] | ['C', 'A', 'B'] | ['C', 'B', 'A']>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Permutation <'B' | 'A' | 'C'>, ['A', 'B', 'C'] | ['A', 'C', 'B'] | ['B', 'A', 'C'] | ['B', 'C', 'A'] | ['C', 'A', 'B'] | ['C', 'B', 'A']>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Permutation <boolean>, [false, true] | [true, false]>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <Permutation <never>, []>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
/*** 这是按顺序做题以来遇到的, 用法都认识, 但就是想不出来解决方案,却又寄希望未知的某个特性一下解决的的那类问题.* 最佳答案解释的很详细, 也有中文翻译.* 但中文版中提供了解决这类问题的最核心一点: 算法* :[Generating Permutations - Topcoder](https://www.topcoder.com/blog/generating-permutations)* 本文的思路就是第一种: remove* BASIC ALGORITHM 1: REMOVE(基础算法之 删除 算法)BASIC ALGORITHM 2: INSERT(基础算法之 插入 算法)BASIC ALGORITHM 3: LEXICOGRAPHIC(基础算法之 字典序 算法)HEAP’S ALGORITHM(高级算法之 堆 算法)*/typeExclude2 <T ,U > =T extendsU ? never :T ;typePermutation <All ,K =All > = [All ] extends [never]? []:K extendsAll ? [K , ...Permutation <Exclude2 <All ,K >>]: never
ts
/*** 这是按顺序做题以来遇到的, 用法都认识, 但就是想不出来解决方案,却又寄希望未知的某个特性一下解决的的那类问题.* 最佳答案解释的很详细, 也有中文翻译.* 但中文版中提供了解决这类问题的最核心一点: 算法* :[Generating Permutations - Topcoder](https://www.topcoder.com/blog/generating-permutations)* 本文的思路就是第一种: remove* BASIC ALGORITHM 1: REMOVE(基础算法之 删除 算法)BASIC ALGORITHM 2: INSERT(基础算法之 插入 算法)BASIC ALGORITHM 3: LEXICOGRAPHIC(基础算法之 字典序 算法)HEAP’S ALGORITHM(高级算法之 堆 算法)*/typeExclude2 <T ,U > =T extendsU ? never :T ;typePermutation <All ,K =All > = [All ] extends [never]? []:K extendsAll ? [K , ...Permutation <Exclude2 <All ,K >>]: never