50. Pow(x, n) 求指数
来源:互联网 发布:猜数字游戏编程java 编辑:程序博客网 时间:2024/04/29 08:10
首先注意n为Integer.MIN_VALUE的情况,取反的时候取不到它的相反数。
使用库函数可以AC;
不使用库函数自己写指数运算的过程要用递归的办法降低时间复杂度,在正负的情况中按奇数偶数分情况讨论。
package leetcode50power;public class Solution {/*double myPow(double x, int n) { int i; double res = 1; if(n==0) return 1; else if(n>0) { return Math.pow(x, n); } else { if(n==Integer.MIN_VALUE) { return 1/(Math.pow(x, Integer.MAX_VALUE)*x); } else return Math.pow(x, n); } }*/ double myPow(double x, int n) { int i; double res = 1; if(n==0) return 1; else if(n>0) { if(n%2==0) { double temp = myPow(x,n>>1); res = temp*temp; } else { double temp = myPow(x,(n-1)>>1); res = temp * temp * x; } } else { if(n==Integer.MIN_VALUE) res = 1 / myPow(x,(int)(Integer.MAX_VALUE*x)); else res = 1 / myPow(x,-n); } return res; }public static void main(String args[]){double x=0.000000000000001;int n=10087787;double res=new Solution().myPow(x,n);System.out.println(res);}}
0 0
- 50. Pow(x, n) 求指数
- Pow(x, n) 指数 @LeetCode
- 求 pow(x, n)
- LeetCode---50. Pow(x, n)(指数运算x的n次方)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50. Pow(x, n)
- 50.Pow(x,n)
- 50. Pow(x, n)
- 蓝鸥iOS开发工程师职场提升路线图
- 给用户的一封信:听说你要结婚了?
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- AVFoundation Programming Guide - Using Assets
- 用户功能<div设计>
- 50. Pow(x, n) 求指数
- React Native Android在模拟器上无法连接Server
- Financial Portfolio Management with Deep Learning
- redis 五种数据类型的使用场景
- 多种数据库查询数据表是否存在
- MD5算法实现
- 伽卡他卡学生端 的卸载!!!
- 【LeetCode】 011. Container With Most Water
- OkHttp完全解析(一)