LeetCode —— Pow(x, n)
来源:互联网 发布:js map foreach 编辑:程序博客网 时间:2024/06/18 05:08
链接:http://leetcode.com/onlinejudge#question_50
原题:
Implement pow(x, n)
思路:1次、2次、4次、8次这样算上去,其实就是n的二进制表示形式。
当然要小心n是负数和底数是+1 和-1的时候,要优化。
代码:
class Solution {public: double pow(double x, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if (x == 1.0) return 1.0; else if (x == -1.0) return n % 2 ? -1 : 1; double answer = 1.0; double base = x; bool negative = n >= 0 ? false : true; n = abs(n); while (n) { if (n & 0x1) answer *= base; n = n >> 1; base = base * base; } if (negative) return 1/answer; else return answer; }};
- LeetCode —— Pow(x, n)
- LeetCode——Pow(x, n)
- leetcode 050 —— 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)
- [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
- 【leetcode】Pow(x, n)
- 从应用中启动另外的应用
- 回调函数的概念及其使用
- spring心得2--bean的生命周期@Spring监听器的作用@Spring初始化容器案例分析@web项目使用
- 在O(1)时间删除链表结点
- static作用(修饰函数、局部变量、全局变量)
- LeetCode —— Pow(x, n)
- hdu3530单调队列(双)
- Linux系统安装Oracle
- Android中利用ant进行多渠道循环批量打包(一)
- 读取SD卡里面的BMP文件 显示到TFT上
- PsExec下载地址及其用法
- compare two string
- log4j
- 第六堂课后作业