当前位置:网站首页>剑指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
边栏推荐
猜你喜欢
![[STM32 Learning 1] Basic knowledge and concepts are clear](/img/1c/7c4fd2d835e15ca13517c6d97e9b3a.png)
[STM32 Learning 1] Basic knowledge and concepts are clear

Summarize computer network super comprehensive test questions

mysql的索引结构为什么选用B+树?

Detailed introduction to the hierarchical method of binary tree creation

基于矩阵计算的线性回归分析方程中系数的估计

4.发布帖子,评论帖子

Use tencent cloud builds a personal blog

将SSE指令转换为ARM NEON指令

MATLAB绘图函数fplot详解

Win11 keeps popping up User Account Control how to fix it
随机推荐
STM32LL library use - SPI communication
二叉排序树与 set、map
Redis常见面试题
轻量化AlphaPose
3.用户上传头像
Win10 cannot directly use photo viewer to open the picture
第二十九章:树的基本概念和性质
质数相关问题-小记
Installation and configuration of Spark and related ecological components - quick recall
Compilation error D8021: Invalid numeric argument '/Wextra' cl command line error d8021 invalid numeric argument '/Wextra'
In-depth understanding of Golang's Map
IPV4和IPV6是什么?
7.Redis
【STM32学习1】基础知识与概念明晰
Introduction to MATLAB drawing functions ezplot explanation
网络安全抓包
MATLAB绘图函数fplot详解
开心一下,9/28名场面合集
基于最小二乘法的线性回归分析方程中系数的估计
深入理解Golang之Map