Skip to content

Commit 7fb80c1

Browse files
committed
feat: update solutions to lc problem: No.0050. Pow(x, n)
1 parent 4f900a6 commit 7fb80c1

File tree

4 files changed

+57
-29
lines changed

4 files changed

+57
-29
lines changed

solution/0000-0099/0050.Pow(x, n)/README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,35 @@
5454
<!-- 这里可写当前语言的特殊实现逻辑 -->
5555

5656
```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
5865
```
5966

6067
### **Java**
6168

6269
<!-- 这里可写当前语言的特殊实现逻辑 -->
6370

6471
```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+
}
6686
```
6787

6888
### **...**

solution/0000-0099/0050.Pow(x, n)/README_EN.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,33 @@
4646
### **Python3**
4747

4848
```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
5057
```
5158

5259
### **Java**
5360

5461
```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+
}
5676
```
5777

5878
### **...**
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
class Solution {
22
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);
55
}
66

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;
1213
}
1314
}
Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
class Solution:
22
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:
124
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

0 commit comments

Comments
 (0)