Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.39 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     double pow(double x, int n) {
  4.         bool negative = false;
  5.         if(n<0){
  6.             negative=true;
  7.             n=-n;
  8.         }
  9.  
  10.         double res=1;
  11.         while(n>0){
  12.             if(n&1==1){
  13.                 res*=x;
  14.                 n--;
  15.             }
  16.             x*=x;
  17.             n >>=1;
  18.         }
  19.         return negative? 1/res : res;
  20.     }
  21. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement