Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9cb5c89

Browse files
committed
Update binary_search.md
快速幂
1 parent 67d78a8 commit 9cb5c89

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎basic_algorithm/binary_search.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,27 @@ class Solution:
133133
else: return False
134134
```
135135

136+
### [Pow(x, n)](https://leetcode.cn/problems/powx-n/)
137+
> 实现 pow(x, n) ,即计算 x 的整数 n 次幂函数(即,x^n )。
138+
139+
思路:使用**快速幂**思想,二分法解决,需要注意奇偶数的不同
140+
141+
**Python版本**
142+
```python
143+
class Solution:
144+
def myPow(self, x: float, n: int) -> float:
145+
if x == 0: return 0
146+
res = 1.0
147+
m = abs(n)
148+
while m > 0:
149+
if m % 2: res = res * x
150+
x = x * x
151+
m = m // 2
152+
153+
if n < 0: return 1. / res
154+
return res
155+
```
156+
136157

137158

138159
### [猜数字大小](https://leetcode.cn/problems/guess-number-higher-or-lower/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /