当前位置:网站首页>剑指 Offer 16. 数值的整数次方
剑指 Offer 16. 数值的整数次方
2022-07-03 12:10:00 【嗝~~~~】
剑指 Offer 16. 数值的整数次方
难度中等310
实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,xn)。不得使用库函数,同时不需要考虑大数问题。
示例 1:
输入:x = 2.00000, n = 10输出:1024.00000
示例 2:
输入:x = 2.10000, n = 3输出:9.26100
示例 3:
输入:x = 2.00000, n = -2输出:0.25000解释:2-2 = 1/22 = 1/4 = 0.25
提示:
- -100.0 < x < 100.0
- -231 <= n <= 231-1
- -104 <= xn <= 104
思路
- 快速幂
- Long N=n
- x=1/x;
代码
class Solution {
public:
double myPow(double x, int n) {
long N=n; //方便转换n,-2147483648也能转为正数
double res=1.0;
if(n<0){
N=-N;
x=1/x; //在计算过程中,不需要再做判断
}
while(N){
int a=N%2;
N>>=1;
if(a==1) res*=x;
x*=x;
}
return res;
}
};
边栏推荐
- How to convert a decimal number to binary in swift
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第六章习题】
- GCN thinking - word2vec directly calculates text classification
- alright alright alright
- Nodejs+express+mysql realizes login function (including verification code)
- 如何在微信小程序中获取用户位置?
- Detailed explanation of the most complete constraintlayout in history
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【SQLServer2012综合练习】
- Enable SASL authentication for memcached
- Dojo tutorials:getting started with deferrals source code and example execution summary
猜你喜欢

01 three solutions to knapsack problem (greedy dynamic programming branch gauge)

Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)

Four problems and isolation level of MySQL concurrency

Node.js: express + MySQL的使用

Seven second order ladrc-pll structure design of active disturbance rejection controller

GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
![[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)](/img/45/c2d7934b886d8090373ca9e6e23c97.gif)
[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)

Detailed explanation of the most complete constraintlayout in history

Sword finger offer14 the easiest way to cut rope

【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
随机推荐
Attack and defense world mobile--ph0en1x-100
ORM use of node -serialize
[exercise 5] [Database Principle]
Enter the length of three sides of the triangle through the user, and calculate the area of the triangle, where the length is a real number
Differences between initial, inherit, unset, revert and all
Dojo tutorials:getting started with deferrals source code and example execution summary
【習題七】【數據庫原理】
Oh my Zsh + TMUX installation
The solution to change the USB flash disk into a space of only 2m
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
Differences and connections between final and static
Keep learning swift
Cache penetration and bloom filter
【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】
Powerful avatar making artifact wechat applet
Analysis of the influence of voltage loop on PFC system performance
Method overloading and rewriting
Everything comes to him who waits
Export the entire Oracle Database