Skip to content

Commit 4f54188

Browse files
committed
Add solution #50
1 parent fd64d2a commit 4f54188

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
36|[Valid Sudoku](./0036-valid-sudoku.js)|Medium|
2121
41|[First Missing Positive](./0041-first-missing-positive.js)|Hard|
2222
43|[Multiply Strings](./0043-multiply-strings.js)|Medium|
23+
50|[Pow(x, n)](./0050-powx-n.js)|Medium|
2324
54|[Spiral Matrix](./0054-spiral-matrix.js)|Medium|
2425
58|[Length of Last Word](./0058-length-of-last-word.js)|Easy|
2526
66|[Plus One](./0066-plus-one.js)|Easy|

solutions/0050-powx-n.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 50. Pow(x, n)
3+
* https://leetcode.com/problems/powx-n/
4+
* Difficulty: Medium
5+
*
6+
* Implement `pow(x, n)`, which calculates `x` raised to the power `n` (i.e., `x^n`).
7+
*/
8+
9+
/**
10+
* @param {number} x
11+
* @param {number} n
12+
* @return {number}
13+
*/
14+
var myPow = function(x, n) {
15+
if (n === 0) return 1;
16+
if (n === 1) return x;
17+
if (n < 0) return 1 / myPow(x, -n);
18+
if (n % 2 === 0) return myPow(x * x, n / 2);
19+
20+
return x * myPow(x * x, (n - 1) / 2);
21+
};

0 commit comments

Comments
 (0)