当前位置:网站首页>Leetcode50. Pow(x, n)
Leetcode50. Pow(x, n)
2022-07-20 07:15:00 【Java full stack R & D Alliance】
Title transmission address : https://leetcode.cn/problems/powx-n/
The code is as follows :
// Solve problems with bit operations
public static double myPow(double x, int n) {
//13=8+4+1 1101
double result = 1;
double carry = x;
boolean sign = true;
long abVal=n; // The reason to use long, Because n May be int The minimum value of a type -2147483648, In this case, the opposite number will overflow
if (n < 0) {
abVal = -abVal;
sign = false;
}
while (abVal > 0) {
int i = (int) (abVal & 1);
abVal = abVal >> 1;
if (i == 1) {
result = result * carry;
}
carry = carry * carry;
}
if (!sign) {
result = (1 / result);
}
return result;
}
边栏推荐
- Share some experience in scaffolding
- Pat class b-b1006 output integer in another format (15)
- Mark and Lightbulbs(思维)
- 接口测试到底怎么做,看完这篇文章彻底搞清楚
- Enterprise digital transformation: three key points!
- Which bank outlet in Henan can buy Ritz fund products?
- 分享搭建脚手架的一些经验
- Reasons for errors in installing wfuzz under win10 CDM
- 独立站流量转化低?客户复购率低?这里看过来
- 【无标题】
猜你喜欢
随机推荐
Mysql_Note1
回归分析模型
2022广东网络安全省赛—代码渗透测试wp
Experiment 2 Advanced film analysis
Typora 过期解决方法,Typora打不开了怎么办
How to realize the overtime abandonment order (several schemes)
Reasons for errors in installing wfuzz under win10 CDM
多线程快速处理List集合(结合线程池的使用)
由pyproject.toml引发的讨论
我们应该测试 DAO 层吗?
由浅入深了解羚珑平台统一接入服务 —— Monet
【无标题】
P3166数三角形(容斥+gcd)
Mysql_Note2
NPE: An FPGA - based overlay Processor for Natural Language
SoC第一个工程hello_world
Video 24 alexnet
Mark and Lightbulbs(思维)
2022-07-18:以下go语言代码输出什么?A:Groutine;B:Main;C:Goroutine;D:GoroutineMain。 package main import ( “f
mysql(34) : 不登录mysql执行sql