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

Commitbf0ced5

Browse files
author
applewjg
committed
Power-n
Change-Id: I82e6ca7e7b9e6831cb65171e1a21d9380f1a1d3b
1 parenta05ec15 commitbf0ced5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎Pow(x,n).java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 25, 2014
4+
Problem: Pow(x, n)
5+
Difficulty: easy
6+
Source: https://oj.leetcode.com/problems/powx-n/
7+
Notes:
8+
Implement pow(x, n).
9+
10+
Solution: recursion.
11+
*/
12+
publicclassSolution {
13+
publicdoublepow(doublex,intn) {
14+
if (x <0)return (n %2 ==0) ?pow(-x,n) : -pow(-x,n);
15+
if (x ==0 ||x ==1)returnx;
16+
if (n <0)return1.0 /pow(x,-n);
17+
if (n ==0)return1.0;
18+
if (n ==1)returnx;
19+
doublehalf =pow(x,n/2);
20+
if (n %2 ==0)returnhalf *half;
21+
elsereturnx *half *half;
22+
}
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp