50. Pow(x, n)
题目:
Implement pow(x, n).
链接: 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)的更多相关文章
- LeetCode - 50. Pow(x, n)
50. Pow(x, n) Problem's Link ----------------------------------------------------------------------- ...
- [Leetcode][Python]50: Pow(x, n)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 50: Pow(x, n)https://leetcode.com/probl ...
- 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 ...
- LeetCode 50. Pow(x, n) 12
50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...
- Java实现 LeetCode 50 Pow(x,n)
50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...
- 刷题-力扣-50. Pow(x, n)
50. Pow(x, n) 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/powx-n/ 著作权归领扣网络所有.商业转载请联系官方授 ...
- [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 ...
- 50. Pow(x, n) (编程技巧)
Implement pow(x, n). double sum = 1; if (n > 0) { while ((n--) > 0) sum *= x; return sum; } el ...
- [Leetcode]50. Pow(x, n)
Implement pow(x, n). 我的做法就比较傻了.排除了所有的特殊情况(而且double一般不可以直接判断==),然后常规情况用循环来做.- -||| 直接用循环,时间复杂度就比较大.应该 ...
随机推荐
- js各类共用方法
function GetParameterValueByName(parametername) { var reg = new RegExp("(^|&)" + param ...
- unpipc.h&unpipc.c
unpipc.h #ifndef _UNPIPC_H #define _UNPIPC_H #include <stdio.h> #include <unistd.h> #inc ...
- linux 标准io笔记
三种缓冲 1.全缓冲:在缓冲区写满时输出到指定的输出端. 比如对磁盘上的文件进行读写通常是全缓冲的. 2.行缓冲:在遇到'\n'时输出到指定的输出端. 比如标准输入和标准输出就是行缓冲, 回车后就会进 ...
- js 前加分号和感叹号是什么意思?
;!function(){}(); ;!有什么用? 从语法上来开,Javascript中分号表示语句结束,在开头加上,可能是为了压缩的时候和别的方法分割一下,表示一个新的语句开始.所以,如果在一个单 ...
- vim命令总结
前言 本文翻译自:http://bencrowder.net/files/vim-fu/,参考了VIM中文帮助. Google翻译结果和实际操作结果,对原文的部分内容重新整理,删除和添加了 部分内容并 ...
- owin 中间件 katana 如何解密cookie
.NET MVC5 默认的用户登录组件是AspNet.Identity ,支持owin,并且微软自己实现的一套owin 中间件叫 katana 补充一下 katana项目源码地址:https://ka ...
- 关于qt5在win7下发布 & 打包
QT5 发布时,莫过于依赖动态链接库(dll) , 但是,QT5的动态链接库貌似都有2套 ,例如 Qt5Core (针对realese) , Qt5Cored (针对debug) ,凡事末尾带d的都是 ...
- cocos2dx中的背景图层CCLayerColor和渐变图层CCLayerGradient
1.CCLayerColor是专门用来处理背景颜色的图层,它继承自CCLayer,可以用来设置图层的背景颜色,因为CCLayer默认是透明色的,即无颜色的 2.CCLayerGradient是用来显示 ...
- 20145120 《Java程序设计》第4周学习总结
20145120 <Java程序设计>第4周学习总结 教材学习内容总结 -定义子类,加"extends+父类名"以继承父类. -子类只能继承一个父类 -编辑器会检查等号 ...
- Notes of the scrum meeting(2013/10/27)
软工项目组buaa_smile确定自由项目主题及实现功能的scrum meeting meeting time:1:00~2:00p.m.,October 27th,2013 meeting plac ...