题目:

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

50. Pow(x, n)的更多相关文章

  1. LeetCode - 50. Pow(x, n)

    50. Pow(x, n) Problem's Link ----------------------------------------------------------------------- ...

  2. [Leetcode][Python]50: Pow(x, n)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 50: Pow(x, n)https://leetcode.com/probl ...

  3. leetcode 50. Pow(x, n) 、372. Super Pow

    50. Pow(x, n) 372. Super Pow https://www.cnblogs.com/grandyang/p/5651982.html https://www.jianshu.co ...

  4. LeetCode 50. Pow(x, n) 12

    50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...

  5. Java实现 LeetCode 50 Pow(x,n)

    50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...

  6. 刷题-力扣-50. Pow(x, n)

    50. Pow(x, n) 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/powx-n/ 著作权归领扣网络所有.商业转载请联系官方授 ...

  7. [LeetCode] 50. Pow(x, n) 求x的n次方

    Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...

  8. 50. Pow(x, n) (编程技巧)

    Implement pow(x, n). double sum = 1; if (n > 0) { while ((n--) > 0) sum *= x; return sum; } el ...

  9. [Leetcode]50. Pow(x, n)

    Implement pow(x, n). 我的做法就比较傻了.排除了所有的特殊情况(而且double一般不可以直接判断==),然后常规情况用循环来做.- -||| 直接用循环,时间复杂度就比较大.应该 ...

随机推荐

  1. 每日一“酷”之difflib

    介绍:difflib 比较序列.该模块包含一些用来计算和处理序列直接差异的工具.她对于比较文本尤其用,其中包含的函数可以使用多种常用差异格式生成报告. 测试数据(splitlines()按行划分为序列 ...

  2. C# sogou地图API应用总结(二)

    在地图上添加自己想要的功能模块 具体代码如下 var map; window.onload = function () { var myOptions = { mapControl: false, / ...

  3. iOS中引用计数内存管理机制分析

    在 iOS 中引用计数是内存的管理方式,虽然在 iOS5 版本中,已经支持了自动引用计数管理模式,但理解它的运行方式有助于我们了解程序的运行原理,有助于 debug 程序. 操作系统的内存管理分成堆和 ...

  4. java & xml parser

    参考: JDK8 API: http://docs.oracle.com/javase/8/docs/api/ DOM: http://www.w3.org/TR/2004/REC-DOM-Level ...

  5. 第一章 Web MVC简介

    Web MVC简介 1.1.Web开发中的请求-响应模型: 在Web世界里,具体步骤如下: 1.  Web浏览器(如IE)发起请求,如访问hao123主页 2.  Web服务器(如Tomcat)接收请 ...

  6. Palindrome Partitioning

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

  7. 团队作业week2-软件分析和用户需求调查

    我们的团队选择评定的软件是必应词典(iphone版)和使用较多的有道词典(iphone版)   类别 描述 评分(Bing) 评分(有道)  功能      核心功能1:词典 顾名思义,作为一款词典类 ...

  8. VS连接远程数据库,连接sqlserver2008,显示“基础提供程序在 Open 上失败”

    今天安装完成VS2012后,在调试2010的程序的时候,出现“基础提供程序在 Open 上失败”,于是用vs连接远程sql2008,才发现问题是:“已成功与服务器连接,但是登录前的握手期间发生错误”, ...

  9. mysql 的存储过程调试软件

    免费下载链接:http://download.csdn.net/detail/meiyoudao/6371137  需要注册 调试还是 F10 类似的

  10. shell uniq sort -u 去重排序

    sort -u 和 uniq都能起到删除重复信息的功能,那么他们的区别究竟在哪呢? $ cat test jason jason jason fffff jason 下面分别执行三个命令 :sort ...