题目:

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. Dataguard之redo传输服务

    一.Data Guard架构 整个Data Guard体系就是围绕三个关键点展开: 日志发送(Redo Send) 日志接收(Redo Receive) 日志应用(Redo Apply) 二.日志发送 ...

  2. KMP算法的理解

    ---恢复内容开始--- 在看数据结构的串的讲解的时候,讲到了KMP算法——一个经典的字符串匹配的算法,具体背景自行百度之,是一个很牛的图灵奖得主和他的学生提出的. 一开始看算法的时候很困惑,但是算法 ...

  3. 利用Jmeter做接口测试

    本文作者:大道测试团队-孙云 1.在安装jmeter之前先配置好JDK,再配置jmeter环境变量. 2.启动jmeter 启动jmeter: 双击Jmeter解压路径(apache-jmeter-3 ...

  4. 系统中使用frameset和Iframe刷新页面session失效

    问题:Asp.net中每次刷新页面,session中保存的只就丢失 原因: 1.有些杀毒软件会去扫描web.config文件 2.程序内部有让session丢失的代码,或服务器内存不足 3.程序有框架 ...

  5. Oralce常用维护命令

    1. sqlplus远程连接 方式一:简易连接,不用进行网络配置,其实就是tnsname.ora文件,但只支持oracle10G以上.命令:sqlplus 用户名/密码@ip地址[:端口]/servi ...

  6. CSS3 transition 属性 过渡效果

    <!DOCTYPE html> <html> <head> <style> div { width:100px; height:100px; backg ...

  7. Log4j配置和简单使用

    Log4j是一款基于Java的开源日志组件,Log4j功能非常强大,我们可以将日志信息输出到控制台.文件.用户界面,也可以输出到操作系统的事件记录器和一些系统常驻进程.更值得一提的是,Log4j可以允 ...

  8. 树形动规--没有上司的舞会--C++

    题目来源:code[VS] 题目描述 Description Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个 ...

  9. XCODE真机调试设备连接一直忙碌如何处理

    只是还没反应过来 等一会就行了

  10. 如何用jmeter对websock和protobuf进行压力测试

    1. 一个websocket插件官网地址 https://github.com/maciejzaleski/JMeter-WebSocketSampler 2. 可以用上述插件,也可以自己扩展,以实现 ...