当前位置:网站首页>剑指 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;
}
};
边栏推荐
- 【数据挖掘复习题】
- Record your vulnhub breakthrough record
- 社交社区论坛APP超高颜值UI界面
- 【Colab】【使用外部数据的7种方法】
- Kotlin - 改良装饰者模式
- 【综合题】【数据库原理】
- Day 1 of kotlin learning: simple built-in types of kotlin
- Public and private account sending prompt information (user microservice -- message microservice)
- Kotlin notes - popular knowledge points asterisk (*)
- 对业务的一些思考
猜你喜欢
![[ArcGIS user defined script tool] vector file generates expanded rectangular face elements](/img/39/0b31290798077cb8c355fbd058e4d3.png)
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements

(latest version) WiFi distribution multi format + installation framework
![[comprehensive question] [Database Principle]](/img/d7/8c51306bb390e0383a017d9097e1e5.png)
[comprehensive question] [Database Principle]

Togaf certification self-study classic v2.0

How to convert a decimal number to binary in swift

社交社区论坛APP超高颜值UI界面
![[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)

Brief introduction to mvcc

【数据库原理及应用教程(第4版|微课版)陈志泊】【SQLServer2012综合练习】

【R】【密度聚类、层次聚类、期望最大化聚类】
随机推荐
社交社区论坛APP超高颜值UI界面
Keep learning swift
【习题七】【数据库原理】
Dix règles de travail
Apache Mina开发手册
Apache Mina Development Manual
Xctf mobile--app1 problem solving
alright alright alright
Attack and defense world mobile--ph0en1x-100
Day 1 of kotlin learning: simple built-in types of kotlin
[problem exploration and solution of one or more filters or listeners failing to start]
Use Tencent cloud IOT platform to connect custom esp8266 IOT devices (realized by Tencent continuous control switch)
Togaf certification self-study classic v2.0
C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep271
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
Sword finger offer14 the easiest way to cut rope
The foreground uses RSA asymmetric security to encrypt user information
GCN thinking - word2vec directly calculates text classification
C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - cases without asynchronous
Swift5.7 扩展 some 到泛型参数