LeetCode 50. Pow(x, n) 12
50. Pow(x, n)
题目描述
实现 pow(x, n),即计算 x 的 n 次幂函数。
每日一算法2019/5/15Day 12LeetCode50. Pow(x, n)
示例 1:
输出: 1024.00000
示例 2:
输出: 9.26100
示例 3:
输出: 0.25000
解释: 2-2 = 1/22 = 1/4 = 0.25
说明:
- -100.0 < x < 100.0
- n 是 32 位有符号整数,其数值范围是 [−231, 231 − 1]。
Java 实现
class Solution {
public double myPow(double x, int n) {
if (x == 1 || n == 0) {
return 1;
}
if (n == Integer.MIN_VALUE) {
if (x == -1) {
return 1;
} else {
return 0;
}
}
if (n < 0) {
n *= -1;
x = 1 / x;
}
return (n % 2 == 0) ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2);
}
}
相似题目
参考资料
LeetCode 50. Pow(x, n) 12的更多相关文章
- LeetCode - 50. Pow(x, n)
50. Pow(x, n) Problem's Link ----------------------------------------------------------------------- ...
- leetcode 50. Pow(x, n) 、372. Super Pow
50. Pow(x, n) 372. Super Pow https://www.cnblogs.com/grandyang/p/5651982.html https://www.jianshu.co ...
- Java实现 LeetCode 50 Pow(x,n)
50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...
- [LeetCode] 50. Pow(x, n) 求x的n次方
Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...
- LeetCode 50 Pow(x, n) (实现幂运算)
题目链接:https://leetcode.com/problems/powx-n/?tab=Description Problem:实现幂运算即 pow(x,n) 设形式为pow(x,n) ...
- LeetCode 50 - Pow(x, n) - [快速幂]
实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10输出: 1024.00000 示例 2: 输入: 2.10000, 3输出: 9.26100 示例 ...
- [leetcode]50. Pow(x, n)求幂
Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Ou ...
- Leetcode——50.Pow(x, n)
@author: ZZQ @software: PyCharm @file: leetcode50_myPow.py @time: 2018/11/22 13:58 要求:实现 pow(x, n) , ...
- Leetcode 50.Pow(x,n) By Python
实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, 3 输出: 9.26100 ...
随机推荐
- java学习笔记(2)注释、public lcass、class、标识符、字面值、变量
java学习笔记(1)中说过了java的一些基础知识,发展史,特点,编译和运行过程,配置环境变量等,接下来开始介绍java的语法等只是点 关于java源程序中的注释: *什么是注释?注释的作用是什么 ...
- http与https区别,get与post请求区别
引用:http://blog.csdn.net/m0_38099607/article/details/72864684 HTTP与HTTPS的区别 超文本传输协议HTTP协议被用于在Web浏览器和网 ...
- NGINX实现咏南跨平台中间件集群
NGINX实现咏南跨平台中间件集群 首先要开启咏南LINUX中间件. 1)编辑usr/local/nginx/conf/nginx.conf #user nobody;worker_processe ...
- Unity3d 错误提示 GUI Error: You are pushing more GUIClips than you are popping. Make sure they are balanced
程序出現這個問題的話,程序編譯時正確,運行時報錯,而且沒有報出是哪個代碼文件出處. 這個問題一般首先去檢查Level內有用到OnGUI,Debug結果發現某代碼文件在調試代碼時複製多了一行GUILay ...
- BeanDefinitionParserDelegate与资源解析
继续上一次的分析 XmlBeanDefinitionReader.java 中 1.registerBeanDefinitions方法 documentReader.registerBeanDefin ...
- openstack kvm cannot set up guest memory 'pc.ram': Cannot allocate memory
Kvm:启动报错:error: internal error: process exited while connecting to monitor: 2018-11-12T01:47:14.9933 ...
- vue报错:There are multiple modules with names that only differ in casing.
今天写项目时,遇到报错信息如下: 经过多次排除及参考网上文章,最后找到问题所在 排查原因:1 .在引用组件时,路径大小写不对也会造成此报错,看例子:错误写法: 正确写法: 2.在组件使用vuex时,引 ...
- 如何使能hyper-v的增强功能?
1. 在hyper-v的设置中使能增强功能 2. 运行在hyper-v中的虚拟机(笔者使用ubuntu版本为bionic)中安装xrdp 2.1 获取安装脚本 $ git clone https:// ...
- Flutter页面跳转返回数据
Dart中的异步请求和等待和ES6中的方法很像,直接使用async...await就可以实现. 核心代码: _navigateToAddress(BuildContext context) async ...
- C2678 二进制“>>”: 没有找到接受“std::stringstream”类型的左操作数的运算符(或没有可接受的转换)
C2678 二进制“>>”: 没有找到接受“std::stringstream”类型的左操作数的运算符(或没有可接受的转换)