当前位置:网站首页>剑指 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;
}
};
边栏推荐
- Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
- Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
- Differences between initial, inherit, unset, revert and all
- Dojo tutorials:getting started with deferrals source code and example execution summary
- Grid connection - Analysis of low voltage ride through and island coexistence
- C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: what is entrustment? P238
- 社交社区论坛APP超高颜值UI界面
- 记录自己vulnhub闯关记录
- 强大的头像制作神器微信小程序
- 01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
猜你喜欢
【R】【密度聚类、层次聚类、期望最大化聚类】
最新版盲盒商城thinkphp+uniapp
ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍
How to convert a decimal number to binary in swift
Record your vulnhub breakthrough record
Differences between initial, inherit, unset, revert and all
2021 autumn Information Security Experiment 1 (password and hiding technology)
电压环对 PFC 系统性能影响分析
对业务的一些思考
【数据库原理及应用教程(第4版|微课版)陈志泊】【SQLServer2012综合练习】
随机推荐
Project video based on Linu development
Togaf certification self-study classic v2.0
Simple use and precautions of kotlin's array array and set list
Bert running error: attributeerror: module'tensorflow contrib. tpu' has no attribute 'InputPipelineConfig'
[Exercice 5] [principe de la base de données]
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: delegatesamplep245
How to stand out quickly when you are new to the workplace?
With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
Nodejs+Express+MySQL实现登陆功能(含验证码)
【習題五】【數據庫原理】
双链笔记·思源笔记综合评测:优点、缺点、评价
[data mining review questions]
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
idea将web项目打包成war包并部署到服务器上运行
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
[combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
Openstack node address change
The latest version of blind box mall thinkphp+uniapp
[judgment question] [short answer question] [Database Principle]
Quick learning 1.8 front and rear interfaces