当前位置:网站首页>剑指Offer 16.数值的整数次方 快速幂+ 递归
剑指Offer 16.数值的整数次方 快速幂+ 递归
2022-08-02 03:33:00 【HotRabbit.】
题目
实现 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
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/shu-zhi-de-zheng-shu-ci-fang-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
没有思路
题解
快速幂+递归:
class Solution {
public double myPow(double x, int n) {
long N = n;
return N > 0 ? quickMul(x,N) : 1.0 / quickMul(x,-N);
}
public double quickMul(double x,long N){
if (N == 0){
return 1.0;
}
double y = quickMul(x,N/2);
return N % 2 == 0 ? y * y : y * y * x;
}
}
边栏推荐
- NSIS来自己设定快捷方式的图标
- 【LeetCode】设计链表
- 2020 - AAAI - Image Inpainting论文导读《Learning to Incorporate Structure Knowledge for Image Inpainting》
- Process (below): process control, termination, waiting, replacement
- Basic IO (on): file management and descriptors
- 向龙芯2K1000板子上烧写中标麒麟系统
- 【LeetCode】合并
- 【面试必看】链表的常见笔试题
- 读取FBX文件踩坑清单
- Anaconda(Jupyter)里发现不能识别自己的GPU该怎么办?
猜你喜欢
随机推荐
【plang1.4.3】语言新特性:集合
基础IO(上):文件管理和描述符
龙讯LT6911系列C/UXC/UXB/GXC/GXB芯片功能区别阐述
【Popular Science Post】UART Interface Communication Protocol
全加器高进位和低进位的理解
将ORCAD原理图导入allegro中进行PCB设计
AD实战篇
电脑基本知识
DMA相应外设映射
2020 - AAAI - Image Inpainting论文导读《Learning to Incorporate Structure Knowledge for Image Inpainting》
【操作系统】线程安全保护机制
【plang 1.4.3】定时器的使用
IDEA2021.2安装与配置(持续更新)
GM7150,振芯科技,视频解码器,CVBS转BT656/601,QFN32,替换TVP5150/CJC5150
引擎开发日志:重构骨骼动画系统
[Arduino connects the clock module to display the time on LCD1602]
谷粒商城10——搜索、商品详情、异步编排
实现动态库(DLL)之间内存统一管理
STM32 CAN 介绍以及相关配置
WebApp 在线编程成趋势:如何在 iPad、Matepad 上编程?