Movatterモバイル変換


[0]ホーム

URL:


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

Commit6d201e6

Browse files
Create 50. Pow(x, n).js
Solution to leetcode 50. Pow(x, n) for JavaScript
1 parent01a2b36 commit6d201e6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎javascript/50. Pow(x, n).js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
*@param {number} x
3+
*@param {number} n
4+
*@return {number}
5+
*/
6+
varmyPow=function(x,n){
7+
// return Math.pow(x,n).toFixed(5) // One line solution
8+
9+
if(x===1.0||n===0)return1
10+
11+
if(n===1)returnx
12+
13+
elseif(n%2===0){
14+
returnmyPow(x*x,n/2)
15+
}
16+
elseif(n%2===1){
17+
returnx*myPow(x,n-1)
18+
}
19+
else{
20+
return1/myPow(x,-n)
21+
}
22+
23+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp