Skip to content

Commit 74e90fb

Browse files
committed
add solution powx-n
1 parent 7746a41 commit 74e90fb

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

go/powx-n.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package leetcode
2+
3+
// https://leetcode.com/problems/powx-n/
4+
5+
import (
6+
"math"
7+
)
8+
9+
var (
10+
pxnBinary []int
11+
)
12+
13+
func searchBinary(x int) uint {
14+
var i uint
15+
length := uint(len(pxnBinary))
16+
for i = 0; i < length; i++ {
17+
if x < pxnBinary[i] {
18+
return i - 1
19+
}
20+
}
21+
return 0
22+
}
23+
24+
func initialBinary() {
25+
pxnBinary = make([]int, 32)
26+
var i uint32
27+
for i = 0; i < 32; i++ {
28+
pxnBinary[i] = 1 << i
29+
}
30+
}
31+
32+
func myPow(x float64, n int) float64 {
33+
resultFlag := 1
34+
if x < 0 {
35+
x = 0 - x
36+
if n&1 == 1 {
37+
resultFlag = -1
38+
}
39+
}
40+
isNNegetive := false
41+
if n < 0 {
42+
n = 0 - n
43+
isNNegetive = true
44+
}
45+
46+
if n == 0 {
47+
return 1
48+
}
49+
if x == 1.00 {
50+
return x * float64(resultFlag)
51+
}
52+
53+
mids := make([]float64, 1, 1024)
54+
mids[0] = x
55+
n64 := int64(n)
56+
var count int64
57+
count = 1
58+
for i := 1; i < n; i++ {
59+
mids = append(mids, mids[i-1]*mids[i-1])
60+
if mids[i] == math.Inf(1) {
61+
if isNNegetive {
62+
return 0
63+
}
64+
return math.Inf(resultFlag)
65+
}
66+
count = count * 2
67+
if count > n64 {
68+
break
69+
}
70+
}
71+
72+
initialBinary()
73+
74+
result := 1.0
75+
left := n
76+
for {
77+
min := searchBinary(left)
78+
// fmt.Printf("left %d, min %d, real %f\n", left, min, mids[min])
79+
result = result * mids[min]
80+
left = left - 1<<min
81+
if left <= 0 {
82+
break
83+
}
84+
}
85+
86+
if isNNegetive {
87+
return 1.0 / result * float64(resultFlag)
88+
}
89+
return result * float64(resultFlag)
90+
}

go/powx-n_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package leetcode
2+
3+
import (
4+
"math"
5+
"testing"
6+
)
7+
8+
func TestMyPow(t *testing.T) {
9+
tests := []struct {
10+
x float64
11+
n int
12+
expect float64
13+
}{
14+
{
15+
x: 2.00000,
16+
n: 10,
17+
expect: 1024.00000,
18+
}, {
19+
x: 2.10000,
20+
n: 3,
21+
expect: 9.26100,
22+
}, {
23+
x: 2.00000,
24+
n: -2,
25+
expect: 0.25000,
26+
}, {
27+
x: 2.00000,
28+
n: 0,
29+
expect: 1.00000,
30+
}, {
31+
x: 100.00000,
32+
n: 1 << 31,
33+
expect: math.Inf(1),
34+
}, {
35+
x: 100.00000,
36+
n: -1 << 31,
37+
expect: 0,
38+
}, {
39+
x: 1.00000,
40+
n: -1 << 31,
41+
expect: 1,
42+
}, {
43+
x: -1.00000,
44+
n: -1 << 31,
45+
expect: 1,
46+
}, {
47+
x: -1.00000,
48+
n: -1<<31 + 1,
49+
expect: -1,
50+
}, {
51+
x: -1.00000,
52+
n: 1,
53+
expect: -1,
54+
}, {
55+
x: -2.00000,
56+
n: 2,
57+
expect: 4,
58+
},
59+
}
60+
for i, item := range tests {
61+
actual := myPow(item.x, item.n)
62+
if math.Abs(actual-item.expect) > 0.00001 {
63+
t.Fatalf("%d expect %f, actual %f", i, item.expect, actual)
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)