diff options
Diffstat (limited to 'pow_xn.cpp')
-rw-r--r-- | pow_xn.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/pow_xn.cpp b/pow_xn.cpp new file mode 100644 index 0000000..f64161f --- /dev/null +++ b/pow_xn.cpp @@ -0,0 +1,20 @@ +#include "leetcode.h" + +class Solution { +public: + double myPow(double x, int n) { + if (n < 0) + x = 1 / x; + long num = labs(n); + double pow = 1; + while (num) { + if (num & 1) + pow *= x; + x *= x; + num >>= 1; + } + return pow; + } +}; + +int main() {} |