zoukankan      html  css  js  c++  java
  • 50. Pow(x, n)

    题目:

    Implement pow(xn).

    链接: http://leetcode.com/problems/powx-n/

    题解:

    使用二分法求实数幂,假如不建立临时变量halfPow,直接return计算结果的话会超时,还没仔细研究为什么。  

    Time Complexity - O(logn), Space Complexity - O(1)。

    public class Solution {
        public double myPow(double x, int n) {
            if(x == 0)
                return x;
            if(n == 0)
                return 1;
            double halfPow = myPow(x, n / 2), result;
            if(n % 2 == 0)
                result = halfPow * halfPow;
            else if (n > 0)
                result = halfPow * halfPow * x;
            else
                result = halfPow * halfPow / x;
            return result;
        }
    }

     更新Update:

    public class Solution {
        public double myPow(double x, int n) {
            if(x == 0.0)
                return 0.0;
            if(n == 0)
                return 1.0;
            if(n % 2 == 0)
                return myPow(x * x, n / 2);
            else {
                if(n > 0)
                    return myPow(x * x, n / 2) * x;
                else
                    return myPow(x * x, n / 2) / x;
            }
        }
    }

    二刷:

    还是二分法。

    Java:

    使用临时变量:

    Time Complexity - O(logn), Space Complexity - O(1)。

    public class Solution {
        public double myPow(double x, int n) {
            if (x == 0.0) {
                return x;
            }
            if (n == 0) {
                return 1;
            }
            double half = myPow(x, n / 2);
            if (n % 2 == 0) {
                return half * half;
            } else if (n > 0) {
                return half * half * x;
            } else {
                return half * half / x;
            }
        }
    }

    不适用临时变量,使用尾递归:

    Time Complexity - O(logn), Space Complexity - O(1)。

    public class Solution {
        public double myPow(double x, int n) {
            if (x == 0.0) {
                return x;
            }
            if (n == 0) {
                return 1;
            }
            if (n % 2 == 0) {
                return myPow(x * x, n / 2);
            } else if (n > 0) {
                return myPow(x * x, n / 2) * x;
            } else {
                return myPow(x * x, n / 2) / x;
            }
        }
    }

    三刷:

    Java:

    public class Solution {
        public double myPow(double x, int n) {
            if (x == 0.0) return 0.0;
            if (n == 0) return 1.0;
            if (n % 2 == 0) return myPow(x * x, n / 2);
            else if (n < 0) return myPow(x * x, n / 2) / x;
            else return myPow(x * x, n / 2) * x;
        }
    }

    测试:

    Reference:

    blog.csdn.net/linhuanmars/article/details/20092829

    https://leetcode.com/discuss/17005/short-and-easy-to-understand-solution

    https://leetcode.com/discuss/52800/5-different-choices-when-talk-with-interviewers

    https://leetcode.com/discuss/12004/my-answer-using-bit-operation-c-implementation

    https://leetcode.com/discuss/9459/o-logn-solution-in-java

    https://leetcode.com/discuss/39143/shortest-python-guaranteed

    https://leetcode.com/discuss/21272/lg-n-320ms-javasolution-9-lines

    https://leetcode.com/discuss/13545/simple-iterative-lg-n-solution

    https://leetcode.com/discuss/62484/iterative-java-python-short-solution-o-log-n

  • 相关阅读:
    Vuex
    浏览器渲染页过程描述
    mvvm 模式
    flex 布局
    js 浮点数计算
    3、异步编程-JS种事件队列的优先级
    高阶函数 debounce 和 throttle
    记录学习MVC过程,HTML铺助类(二)
    记录学习MVC过程,控制器方法和视图(一)
    修改以前项目遇到,所有页面继承BaseBage,Sesssion保存一个model,实现登录(记录一下)
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4436361.html
Copyright © 2011-2022 走看看