Same Tree (C++, Python)
Word Break (C++)

Pow(x, n) (C++)

kitt posted @ 2013年9月24日 01:06 in LeetCode , 1722 阅读

Implement pow(x, n). 一上来就超时了,看了“水中的鱼”的解体报告才知道用二分。

class Solution {
    
    double power(double x, int n)
    {
        if (n == 0) return 1;
        double half = power(x, n/2);
        return (n % 2) ? (half * half * x) : (half * half);
    }
    
public:
    double pow(double x, int n) 
    {
        return (n >= 0) ? power(x, n) : 1.0 / power(x, -n);
    }

};
Avatar_small
zhidanos 说:
2014年9月27日 10:44

说好的python呢 %>_<%


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter