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 fc81271

Browse files
committed
commit solution 50
1 parent b2d4cc6 commit fc81271

File tree

6 files changed

+86
-7
lines changed

6 files changed

+86
-7
lines changed

‎index-tags.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
| [7](https://leetcode-cn.com/problems/reverse-integer) | [整数反转](/solution/1-99/0007.reverse-integer/) | `数学` | <font color=green>简单</font> ||
4949
| [9](https://leetcode-cn.com/problems/palindrome-number) | [回文数](/solution/1-99/0009.palindrome-number/) | `数学` | <font color=green>简单</font> ||
5050
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
51+
| [50](https://leetcode-cn.com/problems/powx-n) | [pow(x, n)](/solution/1-99/0050.pow%28x%2c-n%29/) | `数学`,`二分查找` | <font color=blue>中等</font> ||
5152
| [202](https://leetcode-cn.com/problems/happy-number) | [快乐数](/solution/200-299/0202.happy-number/) | `哈希表`,`数学` | <font color=green>简单</font> ||
5253
| [204](https://leetcode-cn.com/problems/count-primes) | [计数质数](/solution/200-299/0204.count-primes/) | `哈希表`,`数学` | <font color=green>简单</font> ||
5354
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
@@ -106,6 +107,7 @@
106107
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
107108
| --- | --- | --- | --- | --- |
108109
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
110+
| [50](https://leetcode-cn.com/problems/powx-n) | [pow(x, n)](/solution/1-99/0050.pow%28x%2c-n%29/) | `数学`,`二分查找` | <font color=blue>中等</font> ||
109111
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
110112

111113
#### **排序**

‎index-type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@
431431
| [47](https://leetcode-cn.com/problems/permutations-ii) | [全排列 ii](/solution/1-99/0047.permutations-ii/) | `回溯算法` | <font color=blue>中等</font> |
432432
| [48](https://leetcode-cn.com/problems/rotate-image) | [旋转图像](/solution/1-99/0048.rotate-image/) | `数组` | <font color=blue>中等</font> |
433433
| [49](https://leetcode-cn.com/problems/group-anagrams) | [字母异位词分组](/solution/1-99/0049.group-anagrams/) | `哈希表`,`字符串` | <font color=blue>中等</font> ||
434-
| [50](https://leetcode-cn.com/problems/powx-n) | [pow(x, n)](/solution/1-99/0050.pow%28x%2c-n%29/) | `数学`,`二分查找` | <font color=blue>中等</font> |
434+
| [50](https://leetcode-cn.com/problems/powx-n) | [pow(x, n)](/solution/1-99/0050.pow%28x%2c-n%29/) | `数学`,`二分查找` | <font color=blue>中等</font> ||
435435
| [54](https://leetcode-cn.com/problems/spiral-matrix) | [螺旋矩阵](/solution/1-99/0054.spiral-matrix/) | `数组` | <font color=blue>中等</font> |
436436
| [55](https://leetcode-cn.com/problems/jump-game) | [跳跃游戏](/solution/1-99/0055.jump-game/) | `贪心算法`,`数组` | <font color=blue>中等</font> |
437437
| [56](https://leetcode-cn.com/problems/merge-intervals) | [合并区间](/solution/1-99/0056.merge-intervals/) | `排序`,`数组` | <font color=blue>中等</font> |

‎solution/1-99/0050.powx-c-n/README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [50. Pow(x, n)](https://leetcode-cn.com/problems/powx-n)
22

33
### 题目描述
4-
<!-- 这里写题目描述 -->
4+
55
<p>实现&nbsp;<a href="https://www.cplusplus.com/reference/valarray/pow/" target="_blank">pow(<em>x</em>, <em>n</em>)</a>&nbsp;,即计算 x 的 n 次幂函数。</p>
66

77
<p><strong>示例 1:</strong></p>
@@ -32,14 +32,27 @@
3232

3333
### 解题思路
3434

35+
1. 递归解法
36+
2. 当n等于偶数时,特殊处理下
3537

3638
### 具体解法
3739

38-
<!-- tabs:start -->
3940

4041
#### **Golang**
4142
```go
42-
43+
func myPow(x float64, n int) float64 {
44+
if n == 0 {
45+
return 1.00000
46+
}
47+
if n == 1 {
48+
return x
49+
}
50+
if n < 0 {
51+
return myPow(1.0/x, -n)
52+
}
53+
if n%2 != 0 {
54+
return x * myPow(x, n-1)
55+
}
56+
return myPow(x*x, n/2)
57+
}
4358
```
44-
45-
<!-- tabs:end -->

‎solution/1-99/0050.powx-c-n/solution.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=50 lang=golang
5+
*
6+
* [50] Pow(x, n)
7+
*/
8+
9+
// @lc code=start
10+
func myPow(x float64, n int) float64 {
11+
if n == 0 {
12+
return 1.00000
13+
}
14+
if n == 1 {
15+
return x
16+
}
17+
if n < 0 {
18+
return myPow(1.0/x, -n)
19+
}
20+
if n%2 != 0 {
21+
return x * myPow(x, n-1)
22+
}
23+
return myPow(x*x, n/2)
24+
}
25+
26+
// @lc code=end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMyPow(t *testing.T) {
8+
var ret float64
9+
var x float64
10+
var n int
11+
ret = 0
12+
x = 0.00001
13+
n = 2147483647
14+
if ret != myPow(x, n) {
15+
t.Fatalf("case fails %v\n", ret)
16+
}
17+
18+
ret = 1.0000
19+
x = 2
20+
n = 0
21+
if ret != myPow(x, n) {
22+
t.Fatalf("case fails %v\n", ret)
23+
}
24+
25+
ret = 10.0000
26+
x = 10.0000
27+
n = 1
28+
if ret != myPow(x, n) {
29+
t.Fatalf("case fails %v\n", ret)
30+
}
31+
32+
ret = 0.25000
33+
x = 2.0000
34+
n = -2
35+
if ret != myPow(x, n) {
36+
t.Fatalf("case fails %v\n", ret)
37+
}
38+
}

‎solution/1-99/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
- [47. 全排列 ii](solution/1-99/0047.permutations-ii/)
5656
- [48. 旋转图像](solution/1-99/0048.rotate-image/)
5757
- [49. 字母异位词分组 ✅](solution/1-99/0049.group-anagrams/)
58-
- [50. pow(x, n)](solution/1-99/0050.powx-c-n/)
58+
- [50. pow(x, n)](solution/1-99/0050.powx-c-n/)
5959
- [51. n皇后](solution/1-99/0051.n-queens/)
6060
- [52. n皇后 ii](solution/1-99/0052.n-queens-ii/)
6161
- [53. 最大子序和](solution/1-99/0053.maximum-subarray/)

0 commit comments

Comments
(0)

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