[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
Output: 1024.00000
Example 2:
Input: 2.10000, 3
Output: 9.26100
Example 3:
Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
Note:
- -100.0 < x < 100.0
- n is a 32-bit signed integer, within the range [−231, 231 − 1]
这道题让我们求x的n次方,如果只是简单的用个 for 循环让x乘以自己n次的话,未免也把 LeetCode 上的题想的太简单了,一句话形容图样图森破啊。OJ 因超时无法通过,所以需要优化,使其在更有效的算出结果来们可以用递归来折半计算,每次把n缩小一半,这样n最终会缩小到0,任何数的0次方都为1,这时候再往回乘,如果此时n是偶数,直接把上次递归得到的值算个平方返回即可,如果是奇数,则还需要乘上个x的值。还有一点需要引起注意的是n有可能为负数,对于n是负数的情况,我可以先用其绝对值计算出一个结果再取其倒数即可,之前是可以的,但是现在 test case 中加了个负2的31次方后,这就不行了,因为其绝对值超过了整型最大值,会有溢出错误,不过可以用另一种写法只用一个函数,在每次递归中处理n的正负,然后做相应的变换即可,代码如下:
解法一:
class Solution {
public:
double myPow(double x, int n) {
if (n == ) return ;
double half = myPow(x, n / );
if (n % == ) return half * half;
if (n > ) return half * half * x;
return half * half / x;
}
};
这道题还有迭代的解法,让i初始化为n,然后看i是否是2的倍数,不是的话就让 res 乘以x。然后x乘以自己,i每次循环缩小一半,直到为0停止循环。最后看n的正负,如果为负,返回其倒数,参见代码如下:
解法二:
class Solution {
public:
double myPow(double x, int n) {
double res = 1.0;
for (int i = n; i != ; i /= ) {
if (i % != ) res *= x;
x *= x;
}
return n < ? / res : res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/50
类似题目:
参考资料:
https://leetcode.com/problems/powx-n/
https://leetcode.com/problems/powx-n/discuss/19733/simple-iterative-lg-n-solution
https://leetcode.com/problems/powx-n/discuss/19546/Short-and-easy-to-understand-solution
https://leetcode.com/problems/powx-n/discuss/19544/5-different-choices-when-talk-with-interviewers
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 50. Pow(x, n) 求x的n次方的更多相关文章
- 50 Pow(x, n)(求x的n次方Medium)
题目意思:x为double,n为int,求x的n次方 思路分析:直接求,注意临界条件 class Solution { public: double myPow(double x, int n) { ...
- [leetcode]50. Pow(x, n)求幂
Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Ou ...
- [LeetCode] Pow(x, n) 求x的n次方
Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...
- 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: 输入: ...
- LeetCode - 50. Pow(x, n)
50. Pow(x, n) Problem's Link ----------------------------------------------------------------------- ...
- Java实现 LeetCode 50 Pow(x,n)
50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...
- LeetCode 50 - Pow(x, n) - [快速幂]
实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10输出: 1024.00000 示例 2: 输入: 2.10000, 3输出: 9.26100 示例 ...
- LeetCode 50 Pow(x, n) (实现幂运算)
题目链接:https://leetcode.com/problems/powx-n/?tab=Description Problem:实现幂运算即 pow(x,n) 设形式为pow(x,n) ...
随机推荐
- poj-1104 Robbery
Robbery Time Limit: 1000MS Memory Limit: 32768K Total Submissions: 1249 Accepted: 504 Descriptio ...
- Delta-wave HDU - 1030
Delta-wave HDU - 1030 A triangle field is numbered with successive integers in the way shown on the ...
- Netty服务端的启动源码分析
ServerBootstrap的构造: public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, Serve ...
- generator的本质是将异步的管理剥离
generator的本质是将异步的管理剥离
- 使用 PDBDownloader 解决 IDA 加载 ntoskrnl.exe 时符号不完全问题
解决 IDA 加载 ntoskrnl.exe 时符号不完全问题 1. 问题:IDA加载xp系统的 ntoskrnl.exe 加载不完全. 2. 尝试过但未成功的解决方案: 1)配置好的IDA的 pdb ...
- C# if-else 语句
一.简介 一个 if 语句 后可跟一个可选的 else 语句,else 语句在布尔表达式为假时执行. 二.语法 If(判断条件) { 执行的代码: } else { 执行的代码: } 描述: 执行 ...
- 你不知道的Golang盲点汇总【持续更新】
1. 引用传递时append会复制生成新的指针 package main ] ) //append后s指定新复制的内存指针了,不再指向原来的内存 s=append(s, ) fmt.Printf(] ...
- 面试官都叫好的Synchronized底层实现,这工资开多少一个月?
本文为死磕Synchronized底层实现第三篇文章,内容为重量级锁实现. 本系列文章将对HotSpot的synchronized锁实现进行全面分析,内容包括偏向锁.轻量级锁.重量级锁的加锁.解锁.锁 ...
- Error: Opening Robot Framework log failed on mac jenkins
For resolve your problem you must : Connect on your jenkins url (http://[IP]:8080/) Click on Manage ...
- jQuery AJAX方法详谈
AJAX是与服务器交换数据并更新部分网页的技术,而无需重新加载整个页面. 下表列出了所有jQuery AJAX方法: 方法 描述 $.ajax() 执行异步AJAX请求 $.ajaxPrefilter ...