-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path2094-finding-3-digit-even-numbers.js
49 lines (48 loc) · 1.11 KB
/
2094-finding-3-digit-even-numbers.js
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
48
49
/**
* @param {number[]} digits
* @return {number[]}
*/
const findEvenNumbers = function(digits) {
const set = new Set(), visited = new Set()
helper(0, [])
const res = Array.from(set)
res.sort((a, b) => a - b)
return res
function helper(idx, cur) {
if(cur.length === 3) {
set.add(+cur.join(''))
return
}
for(let i = 0; i < digits.length; i++) {
if(visited.has(i)) continue
const d = digits[i]
if(d === 0) {
if(cur.length === 0) continue
else {
cur.push(d)
visited.add(i)
helper(i + 1, cur)
visited.delete(i)
cur.pop()
}
} else {
const isEven = d % 2 === 0
if(cur.length === 3 - 1) {
if(isEven) {
cur.push(d)
visited.add(i)
helper(i + 1, cur)
visited.delete(i)
cur.pop()
} else continue
} else {
cur.push(d)
visited.add(i)
helper(i + 1, cur)
visited.delete(i)
cur.pop()
}
}
}
}
};