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 bef9cf0

Browse files
solves pow(x, n )
1 parent 9f2af59 commit bef9cf0

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
| 47 | [Permutations II](https://leetcode.com/problems/permutations-ii) | [![Java](assets/java.png)](src/PermutationsII.java) | |
5151
| 48 | [Rotate Image](https://leetcode.com/problems/rotate-image) | [![Java](assets/java.png)](src/RotateImage.java) | |
5252
| 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams) | [![Java](assets/java.png)](src/GroupAnagrams.java) | |
53+
| 50 | [Pow(x,n)](https://leetcode.com/problems/powx-n) | [![Java](assets/java.png)](src/Powxn.java) | |
5354
| 53 | [Maximum SubArray](https://leetcode.com/problems/maximum-subarray) | [![Java](assets/java.png)](src/MaximumSubArray.java) [![Python](assets/python.png)](python/maximum_sum_subarray.py) | |
5455
| 55 | [Jump Game](https://leetcode.com/problems/jump-game) | [![Java](assets/java.png)](src/JumpGame.java) | |
5556
| 58 | [Length of Last Word](https://leetcode.com/problems/length-of-last-word) | [![Java](assets/java.png)](src/LengthOfLastWord.java) [![Python](assets/python.png)](python/length_of_last_word.py) | |

‎src/Powxn.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://leetcode.com/problems/powx-n
2+
// T: O(log(n))
3+
// S: O(1)
4+
5+
public class Powxn {
6+
public double myPow(double x, int n) {
7+
if (n >= 0) return power(x, n);
8+
return 1 / power(x, -n);
9+
}
10+
11+
private double power(double x, int n) {
12+
if (n == 0) return 1;
13+
if (n == 1) return x;
14+
double half = power(x, n / 2);
15+
return half * half * (n % 2 == 0 ? 1 : x);
16+
}
17+
}

0 commit comments

Comments
(0)

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