Skip to content

Commit

Permalink
Create power.cpp
Browse files Browse the repository at this point in the history
Beats 100%
  • Loading branch information
gabedonnan authored Mar 30, 2023
1 parent 400defd commit 8f0d7e8
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions power.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
double myPow(double x, int n) {
if (n == 0 || x == 1.0) {
return 1.0;
}

if (n == 1 || x == 0.0) {
return x;
}

if (n == -1) {
return 1.0/x;
}

if (n % 2 == 0) {
return myPow(x * x, n / 2);
} else {
return x * myPow(x, n-1);
}
}
};

0 comments on commit 8f0d7e8

Please sign in to comment.