File tree Expand file tree Collapse file tree 4 files changed +152
-0
lines changed
src/main/java/leetcode/_50_ Expand file tree Collapse file tree 4 files changed +152
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments