50. Pow(x, n)

来源:互联网 发布:tp5 command.php 编辑:程序博客网 时间:2024/04/29 17:31

昨天3-4个小时写了12道题的强度,算是两个月以来最大强度,搞得我脑子直接罢工,下午提前回家,晚上一点做题的想法都没有。。。看来还是阈值太低,还得拉高拉高。。。

明天就是google加面,今天就打算把做过的有google tag的题做一遍。。。到了面试,我觉得应该没有问题,哥的思路的交流风格都比较清晰,应变也还不错,想想google是哥的第一次面试就面得不错,FB第一面也从一开始的懵逼到问清楚问题然后讨论算法,给出代码,在面试官提示下迅速找出bug,总得来说给面试的官的感觉应该是比较反应快,且代码写得还算熟练的,所以,不用慌,自信和心态在有实力的基础上是非常关键的!

这道题,我印象还比较深刻,当时用了2分法实现,再往深里一想,其实也可以3分,4分去实现,所以算法的本质是搞得比较清楚的,就是通过底数的增加和指数的减少来相互调整,回到指数为0的base case,今天再试试能不能一次bug-free


public class Solution {    public double myPow(double x, int n) {/*        if(n==0) return 1; // base case        if(n<0){            if(n==Integer.MIN_VALUE){                return (1/x)*(1/x)*myPow((1/x)*(1/x)*(1/x), (n+2)/(-3));            }            else{                x=1/x;                n=-n;            }        }                if(n%3==0) return myPow(x*x*x, n/3);        else if(n%3==1) return x*myPow(x*x*x, (n-1)/3);        else return x*x*myPow(x*x*x, (n-2)/3);*/                // 果然,一遍bug-free,妥善处理好了Integer.MIN_VALUE的问题。。。同时%达到了将近90% 还是有点NB                // 以下方法非常完美没有问题。。。我睡一觉醒来,觉得这里只是二分,那么其实可以3分,4分,5分等等啊。。。质数次分都可以。。只是在写递归return的时候会复杂一点。。。我不妨来试一下divide by 3,第一个问题就是要解决Integer.MIN_VALUE的问题。。。        if(n==0) return 1; // base case        if(n<0){            if(n==Integer.MIN_VALUE){                n=n/(-2);                x=1/(x*x);            }  // 一开始并没有考虑到Integer.MIN_VALUE的情况,所以出错了。。原因是Integer.MIN_VALUE乘以-1不能变成相反数。。。所以根据下面那段调用递归的分析。。。手动不Integer.MIN_VALUE除以-2,然后通过x=1/(x*x); 这样的变化来适应n的变化。。。思路和递归是一样的。。。不错!!记着吧,这是个关键点!!!就如同spiral matrix 那道题最后的(min%2==1)的判断一样。。。            else{                n=-n;                x=1/x;            }                    }        return (n%2==0)?myPow(x*x, n/2): x*myPow(x*x, (n-1)/2);                  // this is a recursive case... so need the make the variable getting close to the base case. It is beautiful to decrease n by dividing by 2, and adjust the x for the change of n... ALSO! it is important to notice that the base case n==0 will be reached, when n==1, then next is n==0. Before n==1, n could be 2, 3; before 2, 3; n could be 4, 5, 6, 7; before 4,5,6,7, n could be 8, 9, 10, 11, 12, 13, 14, 15        /*        if(n==0) return 1;                int negative=0;        if(n<0){            n*=-1;            negative=1;        }        double result=1.0;  // !!! be careful for the return value type, otherwise, there will be error        for(int i=1; i<=n; i++){            result*=x;        }                if(negative==1){            result=1/result;        }        return result;*/            }}// with the above algo, the time complexcity is O(N),  which cause the the Time Limit Exceeded... so must a better algo with logN.

今天的代码:

public class Solution {    public double myPow(double x, int n) {        if(n==0) return 1;        if(n<0){            if(n==Integer.MIN_VALUE){                n=n/(-2);                x=(1/x)*(1/x);            }            else{                n=-n;                x=1/x;            }        }        return (n%2==0)?myPow(x*x, n/2): x*myPow(x*x, (n-1)/2);    }}



0 0