当前位置:网站首页>剑指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
边栏推荐
- SQL的通用语法和使用说明(图文)
- Mysql之MVCC
- cmake configure libtorch error Failed to compute shorthash for libnvrtc.so
- Exotic curiosity-a solution looking - bit operations
- Summarize computer network super comprehensive test questions
- 第二十八章:解题技巧
- TCP三次握手、四次挥手
- LeetCode 2354. 优质数对的数目 二进制01表示和集合之间的转换
- 推开机电的大门《电路》(一):电压,电流,参考方向
- 网络安全抓包
猜你喜欢
随机推荐
How to simulate 1/3 probability with coins, and arbitrary probability?
Mysql lock
Codeforces Round #605 (Div. 3)
How to add a one-key shutdown option to the right-click menu in Windows 11
A clean start Windows 7?How to load only the basic service start Windows 7 system
cmake configure libtorch error Failed to compute shorthash for libnvrtc.so
轻量化AlphaPose
2021-10-14
Use libcurl to upload the image of Opencv Mat to the file server, based on two methods of post request and ftp protocol
How to set the win10 taskbar does not merge icons
Cmd Markdown 公式指导手册
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
Win10 computer can't read U disk?Don't recognize U disk how to solve?
Win11 computer off for a period of time without operating network how to solve
Doubled and sparse tables
pygame图像连续旋转
jest test, component test
第二十九章:树的基本概念和性质
二叉树创建之层次法入门详解
倍增和稀疏表