当前位置:网站首页>剑指offer:数值的整数次方
剑指offer:数值的整数次方
2022-08-02 14:11:00 【超级码力奥】
直接上快速幂
原题链接:https://www.acwing.com/problem/content/description/26/
class Solution {
public:
double Power(double base, int exponent) {
typedef long long LL;
bool is_minus = exponent < 0;
double res = 1;
for(LL k = abs(LL(exponent)); k; k >>= 1)
{
if(k & 1) res *= base;
base *= base;
}
if(is_minus) res = 1 / res;
return res;
}
};
class Solution(object):
def Power(self, base, exponent):
""" :type base: float :type exponent: int :rtype: float """
is_minus = exponent < 0
res = 1
k = abs(exponent)
while k:
if k & 1:
res *= base
base = base * base
k >>= 1
if is_minus:
res = 1 / res
return res
边栏推荐
- 远程连接Ubuntu中的Mysql
- STM32LL library use - SPI communication
- casbin模型
- Flink + sklearn - use JPMML implement flink deployment on machine learning model
- 5.事务管理
- What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
- KiCad Common Shortcuts
- 二叉树创建之层次法入门详解
- 利用plot_surface命令绘制复杂曲面入门详解
- Win11 computer off for a period of time without operating network how to solve
猜你喜欢
随机推荐
Use libcurl to upload the image of Opencv Mat to the file server, based on two methods of post request and ftp protocol
总结计算机网络超全面试题
General code for pytorch model to libtorch and onnx format
Doubled and sparse tables
Win11 keeps popping up User Account Control how to fix it
Cmd Markdown 公式指导手册
Failed to install using npx -p @storybook/cli sb init, build a dedicated storybook by hand
pygame拖动条的实现方法
推开机电的大门《电路》(一):电压,电流,参考方向
Introduction to MATLAB drawing functions ezplot explanation
Codeforces Round #605 (Div. 3)
GMP scheduling model of golang
Codeforces Round #605 (Div. 3)
Yolov5 official code reading - prior to transmission
6.统一记录日志
背包问题-动态规划-理论篇
【离散化+前缀和】Acwing802. 区间和
第二十七章:时间复杂度与优化
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
Please make sure you have the correct access rights and the repository exists. Problem solved