Skip to content

Commit 135787b

Browse files
committed
leetcode 50
1 parent 87d88a2 commit 135787b

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

0-50/50.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## :pencil2:Leetcode之PHP版题目解析(50. Pow(x, n))
2+
**2020-02-20 吴亲库里 库里的深夜食堂**
3+
****
4+
### :pencil2:描述
5+
**实现 pow(x, n) ,即计算 x 的 n 次幂函数。。**
6+
****
7+
### :pencil2:题目实例
8+
<a href="https://github.com/wuqinqiang/">
9+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/images/50.png">
10+
</a>
11+
****
12+
13+
### :pencil2:题目分析
14+
**这道题可以直观的用暴力法来解决,具体的思路就是模拟整个过程,将值x连乘n次.如果n是负数的话我们可以把x用 1/x 代替,然后将n取绝对值.这种方法的时间复杂度是O(n).还有一种更快的方法,叫快速幂算法,它的时间复杂度为O(logn)**
15+
16+
### :pencil2:
17+
```php
18+
/**
19+
* @param Float $x
20+
* @param Integer $n
21+
* @return Float
22+
*/
23+
function myPow($x, $n) {
24+
if(!$n){
25+
return 1;
26+
}
27+
if ($n <= 0) {
28+
return 1/$this->myPow($x, -$n);
29+
}
30+
if ($n %2) {
31+
return $x * $this->myPow($x, $n-1);
32+
}
33+
34+
return $this->myPow($x*$x, $n/2);
35+
}
36+
```
37+
****
38+
39+
### 联系
40+
41+
<a href="https://github.com/wuqinqiang/">
42+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg" width="150px" height="150px">
43+
</a>
44+
45+
46+
47+
48+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
- [Leetcode47](https://github.com/wuqinqiang/Lettcode-php/blob/master/0-50/47.md)
6666
- [Leetcode48](https://github.com/wuqinqiang/Lettcode-php/blob/master/0-50/48.md)
6767
- [Leetcode49](https://github.com/wuqinqiang/Lettcode-php/blob/master/0-50/49.md)
68+
- [Leetcode50](https://github.com/wuqinqiang/Lettcode-php/blob/master/0-50/50.md)
6869
****
6970

7071
**51-100**

images/50.png

152 KB
Loading

0 commit comments

Comments
 (0)