-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsequential_digits.go
More file actions
48 lines (43 loc) · 873 Bytes
/
Copy pathsequential_digits.go
File metadata and controls
48 lines (43 loc) · 873 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
48
package leetcode
import (
"math"
"sort"
"strconv"
)
func sequentialDigits(low int, high int) []int {
lowD, highD := int(math.Log10(float64(low)))+1, int(math.Log10(float64(high)))+1
res := make([]int, 0, 10)
base := "123456789"
for length := lowD; length <= highD; length++ {
for start := 0; start <= 9-length; start++ {
num, _ := strconv.Atoi(base[start : start+length])
if num >= low && num <= high {
res = append(res, num)
}
}
}
return res
}
func sequentialDigits2(low int, high int) []int {
var backtrack func(int, int)
res := make([]int, 0)
backtrack = func(num, curr int) {
if num > 9 {
return
}
curr = curr*10 + num
if curr >= low {
if curr > high {
return
} else {
res = append(res, curr)
}
}
backtrack(num+1, curr)
}
for i := 1; i < 10; i++ {
backtrack(i+1, i)
}
sort.Ints(res)
return res
}