Skip to content

Commit b09f294

Browse files
committed
970 wrong answer
1 parent 200921a commit b09f294

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed
Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,45 @@
11
package problem0970
22

3+
import (
4+
"math"
5+
"sort"
6+
)
7+
38
func powerfulIntegers(x int, y int, bound int) []int {
9+
res := make([]int, 0, 1024)
10+
11+
logBound := math.Log(float64(bound))
12+
logX := math.Log(float64(x))
13+
logY := math.Log(float64(y))
14+
15+
maxX := int(math.Pow(float64(x), math.Round(logBound/logX)))
16+
maxY := int(math.Pow(float64(y), math.Round(logBound/logY)))
17+
18+
for i := maxX; i >= 1; i /= x {
19+
for j := maxY; j >= 1; j /= y {
20+
if i+j > bound {
21+
continue
22+
}
23+
res = append(res, i+j)
24+
}
25+
}
26+
27+
res = removeRepeated(res)
28+
29+
return res
30+
}
431

5-
return nil
32+
func removeRepeated(nums []int) []int {
33+
sort.Ints(nums)
34+
res := make([]int, 1, len(nums))
35+
res[0] = nums[0]
36+
last := nums[0]
37+
for _, n := range nums {
38+
if n == last {
39+
continue
40+
}
41+
res = append(res, n)
42+
last = n
43+
}
44+
return res
645
}

Algorithms/0970.powerful-integers/powerful-integers_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package problem0970
22

33
import (
4+
"sort"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -14,6 +15,13 @@ var tcs = []struct {
1415
ans []int
1516
}{
1617

18+
{
19+
2,
20+
1,
21+
10,
22+
[]int{2, 3, 4, 5, 8, 9},
23+
},
24+
1725
{
1826
2,
1927
3,
@@ -35,7 +43,10 @@ func Test_powerfulIntegers(t *testing.T) {
3543
ast := assert.New(t)
3644

3745
for _, tc := range tcs {
38-
ast.Equal(tc.ans, powerfulIntegers(tc.x, tc.y, tc.bound), "输入:%v", tc)
46+
sort.Ints(tc.ans)
47+
ans := powerfulIntegers(tc.x, tc.y, tc.bound)
48+
sort.Ints(ans)
49+
ast.Equal(tc.ans, ans, "输入:%v", tc)
3950
}
4051
}
4152

0 commit comments

Comments
 (0)