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 ...
随机推荐
- secureCRT连接服务器和文件传输( 一步搞定)
1.在百度云盘存有此工具,获取到后解压执行即可,如下2 连接目标服务器 192.xxx.xx.xx 2.secureCRT连接服务器和文件传输 ,现象如下 登录后切换到root用户即可有权限操作 ...
- pycharm通过pytest运行报错:No test were found 解决
今天写代码犯了一个不应该犯的小错误,通过记录下来便于查看 1.报错代码如下: platform win32 -- Python 3.7.3, pytest-4.0.2, py-1.8.0, plugg ...
- Spring MVC原理及配置
Spring MVC原理及配置 1. Spring MVC概述 Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得 ...
- input的禁止标签
<body> <input type="text" name="" value="你好" disabled="d ...
- Shell编程——脚本编写思路与过程
Linux系统Shell编程——脚本编写思路与过程 “ 前段时间有小伙伴问我一些问题,涉及到shell脚本的编写问题,事后,我深入思考了下,实际生产环境的确也会经常用到,因此如何写这个脚本?它的思路在 ...
- 4、vueJs基础知识04
简单的目录结构: |-index.html |-main.js 入口文件 |-App.vue vue文件(组件),官方推荐命名法(首字母大写) |-components 组件存放的文件夹 | ...
- python 生成 pyc 文件
以 pyc 为扩展名的是Python的编译文件.其执行速度快于 py 文件且不能用文本编辑编辑查看.所以 pyc 文件往往代替 py 文件发布. Python 在执行时,首先会将 py 文件中的源代码 ...
- python 画园和椭圆
from matplotlib.patches import Ellipse, Circle import matplotlib.pyplot as plt fig = plt.figure() ax ...
- 关于Delphi中二维数组的声明和大小调整
这是一个实例: procedure TMainForm.Button1Click(Sender: TObject);var arr:array of array of string;begin s ...
- Cesium - Fabric 材质【转官网】
https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric Fabric Hannah edited this page on 24 Dec ...