当前位置:网站首页>Sword finger offer 16 Integer power of numeric value
Sword finger offer 16 Integer power of numeric value
2022-07-03 12:58:00 【Hiccup~~~~】
The finger of the sword Offer 16. Integer power of value
Medium difficulty 310
Realization pow(x, n) , Computation x Of n Power function ( namely ,xn). Do not use library functions , At the same time, there is no need to consider the problem of large numbers .
Example 1:
Input :x = 2.00000, n = 10 Output :1024.00000
Example 2:
Input :x = 2.10000, n = 3 Output :9.26100
Example 3:
Input :x = 2.00000, n = -2 Output :0.25000 explain :2-2 = 1/22 = 1/4 = 0.25
Tips :
- -100.0 < x < 100.0
- -231 <= n <= 231-1
- -104 <= xn <= 104
Ideas
- Fast power
- Long N=n
- x=1/x;
Code
class Solution {
public:
double myPow(double x, int n) {
long N=n; // Easy conversion n,-2147483648 Can also be converted to a positive number
double res=1.0;
if(n<0){
N=-N;
x=1/x; // During calculation , There is no need to make judgment
}
while(N){
int a=N%2;
N>>=1;
if(a==1) res*=x;
x*=x;
}
return res;
}
};
边栏推荐
- alright alright alright
- 初入职场,如何快速脱颖而出?
- 我的创作纪念日:五周年
- Grid connection - Analysis of low voltage ride through and island coexistence
- Xctf mobile--rememberother problem solving
- Xctf mobile--app2 problem solving
- Powerful avatar making artifact wechat applet
- 【习题五】【数据库原理】
- C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous
- Kung Fu pays off, and learning is done
猜你喜欢
强大的头像制作神器微信小程序
[comprehensive question] [Database Principle]
Detailed explanation of the most complete constraintlayout in history
Node. Js: use of express + MySQL
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
Analysis of the influence of voltage loop on PFC system performance
Xctf mobile--app2 problem solving
Xctf mobile--app1 problem solving
[data mining review questions]
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
随机推荐
luoguP3694邦邦的大合唱站队
Glide question you cannot start a load for a destroyed activity
最新版抽奖盲盒运营版
Seven second order ladrc-pll structure design of active disturbance rejection controller
[review questions of database principles]
[combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number
【数据库原理复习题】
剑指 Offer 14- I. 剪绳子
【R】【密度聚类、层次聚类、期望最大化聚类】
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
Cache penetration and bloom filter
Swift Error Handling
ORM use of node -serialize
Deeply understand the mvcc mechanism of MySQL
十條職場規則
Solve the problem of VI opening files with ^m at the end
[problem exploration and solution of one or more filters or listeners failing to start]
Low code platform international multilingual (I18N) technical solution
【习题五】【数据库原理】
有限状态机FSM