Pow(x, n)
来源:互联网 发布:2016nba西决数据统计 编辑:程序博客网 时间:2024/06/07 09:30
50. Pow(x, n)
- Total Accepted: 98575
- Total Submissions: 356042
- Difficulty: Medium
Implement pow(x, n).
Subscribe to see which companies asked this question
Show Similar Problems
Have you met this question in a real interview?
Yes
No
Discuss Pick One
package com.lp;public class Solution { public double myPow(double x, int n) { if(n==0) return 1; if(x==1) return 1; long nn=(long)n; if(n<0) { nn=-1l*n;//注意别漏了l,-1*-2147483648=-2147483648 x=1/x; } return myPositivePow(x, nn); } /* * 如:2^5,普通循环乘2,要循环五次。 * 如下只需要log5+1. * 0000,0101 */ public double myPositivePow(double x,long n){ double sum = 1; double pre = x; while(n!=0){ if((n&1)==1) { sum=sum*pre;//如果当前位为1,需要累乘起来。 } pre=pre*pre;//翻倍增长 n=n>>1; } return sum; } public static void main(String[] args) {Solution solution = new Solution();System.out.println(solution.myPow(2, -2147483648));}}
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)
- 屏幕的适配-新建相应分辨率文件夹
- 项目中出现java.lang.NoSuchMethodException 继续整理
- 防xss攻击总结
- 获取form表单所有输入内容
- Jetty简介
- Pow(x, n)
- Android中ListView实现多级分组显示的高效实现
- 交叉编译器制作
- php 根据某数组key计算和
- sobel算子原理与实现
- 如何使用Spark ALS实现协同过滤
- 1028. 人口普查(20)
- 团体程序设计天梯赛L3-008——喊山(bfs)
- Android 如何让EditText不自动获取焦点