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一般不可以直接判断==),然后常规情况用循环来做.- -||| 直接用循环,时间复杂度就比较大.应该 ...
随机推荐
- 我的第一个MVC4程序
InputExtensions 方法解释 http://blog.csdn.net/cnceohjm/article/details/8936669 https://msdn.microsoft.co ...
- apache ab的安装步骤
1:到apache官方网站http://httpd.apache.org/download.cgi#apache24下载最新版本的apache,然后解压,执行如下命令: ./configure –pr ...
- ActiveMQ使用记录
1.在Linux中安装ActiveMQ 官方文档地址:http://activemq.apache.org/getting-started.html#GettingStarted-StartingAc ...
- PHP中日期时间函数date()用法总结
date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考. 格式化日期date() 函数的第一个参数规定了如何格式化日期/时间.它使用字母 ...
- 【个人笔记】001-PHP基础-01-PHP快速入门-01-PHP职业路线及PHP前景
001-PHP基础-01-PHP快速入门 01-PHP职业路线及PHP前景 PHP职业路线 PHP初级工程师 1年以下 3k-6k PHP中级工程师 1-3年6k-10k PHP高级工程师 3年以上 ...
- 1072: [SCOI2007]排列perm - BZOJ
Description 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0).例如123434有90种排列能被2整除,其中末位为2的有30种,末位为4的有60种.Input ...
- HDU 5486 Difference of Clustering 图论
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5486 题意: 给你每个元素一开始所属的集合和最后所属的集合,问有多少次集合的分离操作,并操作和不变操 ...
- 【转】欧拉回路&特殊图下的哈密顿回路题集
转自:http://blog.csdn.net/shahdza/article/details/7779385 欧拉回路[HDU]1878 欧拉回路 判断3018 Ant Trip 一笔画问题1116 ...
- 【BZOJ】【2208】【JSOI2010】连通数
题解: 1.Tarjan缩点以后对每个连通分量进行深搜,看能到哪些连通分量,能到达的所有连通分量的size之和记为sum.则第i个连通分量对答案的贡献为size[i]*sum(到其他连通分量)+siz ...
- Matlab实现二进制矩阵转换为十进制
一.问题描述 [1 1 1 0 1 0 1 1 0 1 0 0 1 1 0] 每两位3转换为一个十进制数,共5列,那么转换后是ceil(5/3)=2列. [7 1 6 1 1 2] 二.问题分析 1. ...