File tree Expand file tree Collapse file tree 4 files changed +57
-29
lines changed
solution/0000-0099/0050.Pow(x, n) Expand file tree Collapse file tree 4 files changed +57
-29
lines changed Original file line number Diff line number Diff line change 54
54
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
55
56
56
``` python
57
-
57
+ class Solution :
58
+ def myPow (self , x : float , n : int ) -> float :
59
+ if n == 0 :
60
+ return 1
61
+ if n < 0 :
62
+ return 1 / self .myPow(x, - n)
63
+ y = self .myPow(x, n >> 1 )
64
+ return y * y if (n & 1 ) == 0 else y * y * x
58
65
```
59
66
60
67
### ** Java**
61
68
62
69
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
70
64
71
``` java
65
-
72
+ class Solution {
73
+ public double myPow (double x , int n ) {
74
+ long N = n;
75
+ return N >= 0 ? pow(x, N ) : 1.0 / pow(x, - N );
76
+ }
77
+
78
+ public double pow (double x , long N ) {
79
+ if (N == 0 ) {
80
+ return 1.0 ;
81
+ }
82
+ double y = pow(x, N >> 1 );
83
+ return (N & 1 ) == 0 ? y * y : y * y * x;
84
+ }
85
+ }
66
86
```
67
87
68
88
### ** ...**
Original file line number Diff line number Diff line change 46
46
### ** Python3**
47
47
48
48
``` python
49
-
49
+ class Solution :
50
+ def myPow (self , x : float , n : int ) -> float :
51
+ if n == 0 :
52
+ return 1
53
+ if n < 0 :
54
+ return 1 / self .myPow(x, - n)
55
+ y = self .myPow(x, n >> 1 )
56
+ return y * y if (n & 1 ) == 0 else y * y * x
50
57
```
51
58
52
59
### ** Java**
53
60
54
61
``` java
55
-
62
+ class Solution {
63
+ public double myPow (double x , int n ) {
64
+ long N = n;
65
+ return N >= 0 ? pow(x, N ) : 1.0 / pow(x, - N );
66
+ }
67
+
68
+ public double pow (double x , long N ) {
69
+ if (N == 0 ) {
70
+ return 1.0 ;
71
+ }
72
+ double y = pow(x, N >> 1 );
73
+ return (N & 1 ) == 0 ? y * y : y * y * x;
74
+ }
75
+ }
56
76
```
57
77
58
78
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public double myPow (double x , int n ) {
3
- if ( n < 0 ) return sum ( 1.0 / x , 0 - n ) ;
4
- return sum (x ,n );
3
+ long N = n ;
4
+ return N >= 0 ? pow (x , N ) : 1.0 / pow ( x , - N );
5
5
}
6
6
7
- public double sum (double x , int n ) {
8
- if (n == 0 ) return 1 ;
9
- if (n == 1 ) return x ;
10
- if ( n % 2 == 0 ) return sum (x * x , n / 2 );
11
- else return x * sum (x * x , n / 2 );
7
+ public double pow (double x , long N ) {
8
+ if (N == 0 ) {
9
+ return 1.0 ;
10
+ }
11
+ double y = pow (x , N >> 1 );
12
+ return (N & 1 ) == 0 ? y * y : y * y * x ;
12
13
}
13
14
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def myPow (self , x : float , n : int ) -> float :
3
-
4
- """
5
- :type x: float
6
- :type n: int
7
- :rtype: float
8
- """
9
-
10
- answer = 1
11
- if x == 1 or n == 0 :
3
+ if n == 0 :
12
4
return 1
13
- if x == - 1 :
14
- return 1 if n % 2 == 0 else - 1
15
-
16
- for i in range (abs (n )):
17
- answer *= x
18
- if (abs (answer ) < 10 ** - 5 and n > 0 ) or (abs (answer ) > 10 ** 5 and n < 0 ):
19
- return 0
20
-
21
- return answer if n > 0 else 1 / answer
5
+ if n < 0 :
6
+ return 1 / self .myPow (x , - n )
7
+ y = self .myPow (x , n >> 1 )
8
+ return y * y if (n & 1 ) == 0 else y * y * x
You can’t perform that action at this time.
0 commit comments