Pow(x, n)
来源:互联网 发布:windows xp系统是什么 编辑:程序博客网 时间:2024/06/08 12:27
用到了快速幂原理:
以下以求a的b次方来介绍
把b转换成二进制数。
该二进制数第i位的权为
例如
11的二进制是1011
11 = 2³×1 + 2²×0 + 2¹×1 + 2º×1
因此,我们将a¹¹转化为算
提交的时候遇到一个corner case,n=-INT_MAX-1,解决方法是直接用long long转存,简单粗暴
提交的时候遇到一个corner case,n=-INT_MAX-1,解决方法是直接用long long转存,简单粗暴
class Solution {public:double myPow(double x, int n) {if (x == 1 || n == 0)return 1;long long lln = n;if (lln<0){lln = -lln;x = 1 / x;}double res, tmp;res = 1; tmp = x;while (lln != 0) {if (lln & 1 == 1) { res = res*tmp; }tmp = tmp*tmp; lln = lln >> 1;}return res;}};
0 0
- Pow(x, n) / Super Pow
- LeetCode: Pow(x, n)
- LeetCode Pow(x,n)
- [Leetcode] Pow(x, n)
- LeetCode: Pow(x, n)
- Leetcode: Pow(x,n)
- [LeetCode] Pow(x, n)
- [Leetcode] Pow(x,n)
- Pow(x, n)
- 【leetcode】Pow(x, n)
- Pow(x, n)
- 求 pow(x, n)
- [LeetCode]Pow(x, n)
- Pow(x,n)
- Pow(x, n)
- Pow(x, n)
- [leetcode]Pow(x, n)
- LeetCode-Pow(x, n)
- 第五周:Raptor:三色球问题
- Servlet规范总结
- iOS字体及样式
- [JAVA学习笔记-60]ByteBuffer的分配方式的比较
- OpenCV读取的图片从BGR转换为RGB
- Pow(x, n)
- PHP数据对象映射模式与实现简单的链式操作
- 第五周—三个数中的最大值
- 第五周项目9斐波那契数列
- R Notebooks
- Jetty9架构解析-Jetty介绍
- 【构造函数】----构造函数里的变量--内存图解
- 欢迎使用CSDN-markdown编辑器
- C语言字符串的基本知识