-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
76 lines (67 loc) · 1.16 KB
/
main.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
func fs1(from, to int) int {
sum := 0
for i := from; i <= to; i++ {
digits := toDigits(i)
if twoAdjacentDigits(digits) && neverDecrease(digits) {
sum++
}
}
return sum
}
func toDigits(i int) []int {
var v []int
for i != 0 {
v = append(v, i%10)
i /= 10
}
res := make([]int, len(v))
for idx := 0; idx < len(v); idx++ {
res[idx] = v[len(v)-idx-1]
}
return res
}
func twoAdjacentDigits(digits []int) bool {
for i := 1; i < len(digits); i++ {
if digits[i] == digits[i-1] {
return true
}
}
return false
}
func twoExactDigits(digits []int) bool {
for i := 1; i < len(digits); i++ {
if digits[i] == digits[i-1] {
if i > 1 {
if digits[i-2] == digits[i] {
continue
}
}
if i < len(digits)-1 {
if digits[i] == digits[i+1] {
continue
}
}
return true
}
}
return false
}
func neverDecrease(digits []int) bool {
for i := 1; i < len(digits); i++ {
if digits[i] < digits[i-1] {
return false
}
}
return true
}
func fs2(from, to int) int {
sum := 0
for i := from; i <= to; i++ {
digits := toDigits(i)
if twoExactDigits(digits) && neverDecrease(digits) {
sum++
}
}
return sum
}