forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
29 lines (27 loc) · 765 Bytes
/
Solution.cs
File metadata and controls
29 lines (27 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace LeetCodeNet.G0001_0100.S0050_powx_n {
// #Medium #Top_Interview_Questions #Math #Recursion #Udemy_Integers #Top_Interview_150_Math
// #2025_07_02_Time_0_ms_(100.00%)_Space_29.32_MB_(55.95%)
public class Solution {
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822", Justification = "LeetCode")]
public double MyPow(double x, int n) {
long nn = n;
double res = 1;
if (n < 0) {
nn *= -1;
}
while (nn > 0) {
if ((nn % 2) == 1) {
nn--;
res *= x;
} else {
x *= x;
nn /= 2;
}
}
if (n < 0) {
return 1.0 / res;
}
return res;
}
}
}