Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- knakul853
- **/
- class Solution {
- public:
- double myPow(double x, int n) {
- long p = (long)n;
- if( n < 0 )
- return 1.0 / binPow(x, -p);
- else
- return binPow(x, p);
- }
- double binPow( double x, long p )
- {
- double res = 1.0;
- while( p )
- {
- if( p&1 )
- res = res * x;
- p = p/2;
- x = x * x;
- }
- return res;
- }
- };
Add Comment
Please, Sign In to add comment