leetcode Pow(x,n)
来源:互联网 发布:普通话考试难不难知乎 编辑:程序博客网 时间:2024/06/13 22:52
题目:https://oj.leetcode.com/problems/powx-n/
题意:实现次方运算
思路:1:注意INT_MIN这个情况,当n取整数最小值时,他的相反数是INT_MAX*x
2,:参考别人用二分法求解,时间复杂度变为logn
3:百思不得其解,当下面的程序不用double v去取代pow的结果时,就会报错
代码:
class Solution {public: double pow(double x, int n) { if(n==0) return 1.0; if(n<0){ if(n == INT_MIN) return 1.0 / (pow(x,INT_MAX)*x); else return 1.0/pow(x,-n); } double v = pow(x,n/2) ; if (n % 2 == 0) return v * v; else return v*v* x; }};
还有我一直不知道我怎么错了的代码:
class Solution {public: double pow(double x, int n) { if(n==0) return 1.0; if(n<0){ if(n == INT_MIN) return 1.0 / (pow(x,INT_MAX)*x); else return 1.0/pow(x,-n); } double v = pow(x,n-1); return x*v; }};
0 0
- 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)
- 【Leetcode】Pow(x, n)
- LeetCode Pow(x, n)
- Leetcode: Pow(x, n)
- 日经社説 20150221 企業は資本効率の向上で市場評価高めよ
- 社説 20150221 医療保険改革 都道府県主導で効率化図ろう
- 社説 20150221 対テロ閣僚会合 重層的な包囲網の構築を急げ
- Mysql导入大文件及导入中文乱码问题解决
- ASP.net/C#中如何调用动态链接库DLL
- leetcode Pow(x,n)
- 播放gif 最简单直接的方法,但是方便灵活的用
- shader漫反射模拟
- C/S和B/S和RIA
- solr
- oc地图
- 一组python用于验证数据的函数
- TCP、UDP和HTTP
- HDU 3613 Best Reward(求前后缀回文 拓展KMP or Manacher)