Skip to content

Commit 3edbc89

Browse files
committed
50. Pow(x, n)
1 parent cc30236 commit 3edbc89

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

markdown/50. Pow(x, n).md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
### [50\. Pow(x, n)](https://leetcode.com/problems/powx-n/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Implement , which calculates _x_ raised to the power _n_ (x<sup><span style="font-size: 10.8333px; display: inline;">n</span></sup>).
7+
8+
**Example 1:**
9+
10+
```
11+
Input: 2.00000, 10
12+
Output: 1024.00000
13+
```
14+
15+
**Example 2:**
16+
17+
```
18+
Input: 2.10000, 3
19+
Output: 9.26100
20+
```
21+
22+
**Example 3:**
23+
24+
```
25+
Input: 2.00000, -2
26+
Output: 0.25000
27+
Explanation: 2-2 = 1/22 = 1/4 = 0.25
28+
```
29+
30+
**Note:**
31+
32+
* -100.0 < _x_ < 100.0
33+
* _n_ is a 32-bit signed integer, within the range [−2<sup>31</sup>, 2<sup>31 </sup>− 1]
34+
35+
36+
#### Solution
37+
38+
Language: **Java**
39+
40+
```java
41+
class Solution {
42+
   public double myPow(double x, int n) {
43+
       if (n == 0) { // 任何数的零次幂都是 1
44+
           return 1;
45+
      }
46+
       if (n < 0) { // 如果 n 是负数,先把负数提到 x 里面,然后再计算
47+
           if (n == Integer.MIN_VALUE) {
48+
               n++;  // 这里需要考虑 INT_MAX < -INT_MIN ,防止溢出
49+
               x = x * x;// 同时需要把少乘的那个数字乘进去
50+
          }
51+
           n = -n; // 这里需要考虑 INT_MAX < -INT_MIN ,所以需要处理一下
52+
           x = 1 / x;
53+
      }
54+
       // 递归
55+
       return (n % 2 == 0) ? myPow(x * x, n >> 1) : x * myPow(x * x, n >> 1);
56+
  }
57+
}
58+
```
59+
![](http://ww1.sinaimg.cn/large/006tNc79ly1g4v32p2fbsj30v10u0dnf.jpg)

src/main/java/leetcode/_50_/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package leetcode._50_;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
System.out.println(solution.myPow(2.0, 10));
12+
System.out.println(Integer.MIN_VALUE);
13+
}
14+
}
15+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode._50_;
2+
3+
class Solution {
4+
public double myPow(double x, int n) {
5+
if (n == 0) { // 任何数的零次幂都是 1
6+
return 1;
7+
}
8+
if (n < 0) { // 如果 n 是负数,先把负数提到 x 里面,然后再计算
9+
if (n == Integer.MIN_VALUE) {
10+
n++; // 这里需要考虑 INT_MAX < -INT_MIN ,防止溢出
11+
x = x * x;// 同时需要把少乘的那个数字乘进去
12+
}
13+
n = -n; // 这里需要考虑 INT_MAX < -INT_MIN ,所以需要处理一下
14+
x = 1 / x;
15+
}
16+
// 递归
17+
return (n % 2 == 0) ? myPow(x * x, n >> 1) : x * myPow(x * x, n >> 1);
18+
}
19+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
### [50\. Pow(x, n)](https://leetcode.com/problems/powx-n/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Implement , which calculates _x_ raised to the power _n_ (x<sup><span style="font-size: 10.8333px; display: inline;">n</span></sup>).
7+
8+
**Example 1:**
9+
10+
```
11+
Input: 2.00000, 10
12+
Output: 1024.00000
13+
```
14+
15+
**Example 2:**
16+
17+
```
18+
Input: 2.10000, 3
19+
Output: 9.26100
20+
```
21+
22+
**Example 3:**
23+
24+
```
25+
Input: 2.00000, -2
26+
Output: 0.25000
27+
Explanation: 2-2 = 1/22 = 1/4 = 0.25
28+
```
29+
30+
**Note:**
31+
32+
* -100.0 < _x_ < 100.0
33+
* _n_ is a 32-bit signed integer, within the range [−2<sup>31</sup>, 2<sup>31 </sup>− 1]
34+
35+
36+
#### Solution
37+
38+
Language: **Java**
39+
40+
```java
41+
class Solution {
42+
   public double myPow(double x, int n) {
43+
       if (n == 0) { // 任何数的零次幂都是 1
44+
           return 1;
45+
      }
46+
       if (n < 0) { // 如果 n 是负数,先把负数提到 x 里面,然后再计算
47+
           if (n == Integer.MIN_VALUE) {
48+
               n++;  // 这里需要考虑 INT_MAX < -INT_MIN ,防止溢出
49+
               x = x * x;// 同时需要把少乘的那个数字乘进去
50+
          }
51+
           n = -n; // 这里需要考虑 INT_MAX < -INT_MIN ,所以需要处理一下
52+
           x = 1 / x;
53+
      }
54+
       // 递归
55+
       return (n % 2 == 0) ? myPow(x * x, n >> 1) : x * myPow(x * x, n >> 1);
56+
  }
57+
}
58+
```
59+
![](http://ww1.sinaimg.cn/large/006tNc79ly1g4v32p2fbsj30v10u0dnf.jpg)

0 commit comments

Comments
 (0)