-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigits_combination.js
More file actions
47 lines (42 loc) · 871 Bytes
/
digits_combination.js
File metadata and controls
47 lines (42 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 穷举一个数字的组成的全部排列组合形式,例如:
* 123,
* 132,
* 213,
* 231,
* 312,
* 321.
*/
function digitsCombination(num) {
if (num < 10) { // one bit number.
return num
}
const numsStr = '' + num
const map = Object.create(null)
for (let item of numsStr) {
const restChars = numsStr.split(item)
let allChars = ''
for (let chars of restChars) {
allChars += chars
}
map[item] = digitsCombination(allChars)
}
return map
}
function deepIterate(tree) {
let str = ''
Object.keys(tree).forEach((key) => {
const value = tree[key]
str += key
if (typeof value === 'string') {
str += value
} else {
str += deepIterate(value)
}
})
console.log(str)
return str
}
const tree = digitsCombination(987643)
const result = deepIterate(tree)
// console.log(result)