leetcode-50-pow()
题目描述:

方法一:O(logn)递归:
class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
if n < 0:
return 1/self.myPow(x,-n)
if n&1:
return x*self.myPow(x,n-1)
return self.myPow(x*x,n//2)
迭代:
class Solution:
def myPow(self, x: float, n: int) -> float:
if n < 0:
x,n = 1/x,-n
ans = 1
while n > 0:
if n&1:
ans *= x
x *= x
n >>= 1
return ans
java版:
class Solution {
public double myPow(double x, int n) {
if(x==0) return 0;
long b = n;
double res = 1.0;
if(b<0){
x = 1 / x;
b = -b;
}
while(b>0){
if((b&1)==1) res *= x;
x *= x;
b >>= 1;
}
return res;
}
}
leetcode-50-pow()的更多相关文章
- LeetCode 50. Pow(x, n) 12
50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...
- 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 ...
随机推荐
- 关于版本管理工具SVN
曾经使用过Git,但是目前使用的是相对简单的svn. 刚使用的时候,如果不出意外.会有同学配好环境与权限. 前端只需要将代码下载,修改,更新,与上传. 一 .下载 1.本地新建文件夹,作为本地仓库 命 ...
- 关于EntityFramework 更新数据记录时字段全部更新问题和不从数据库中获取直接更新记录
一.一直对这个比较疑惑感觉只修改一条数据记录的一个字段结果更新Savechages后跟踪生成sql竟然是全部被修改,感觉微软怎么这么傻,总觉得会有其它方式可以只更新部分字段,但一直没有找到相关设置,最 ...
- HTML5字体、伪元素、背景
1.字体样式: 选择器 { font:font-style font-weight font-size / line-height font-family:} 例:p { font: ita ...
- spring mvc 配置后,web中的html页面报404,该怎么处理
问题描述: 在根目录webapp下的jsp页面可以通过url直接访问,而html页面就会报404错误. 解决方案1: 在spring-mvc.xml中添加如下配置: <!--将静态文件指定到某个 ...
- 2019-4-29-.NET-Standard
title author date CreateTime categories .NET Standard lindexi 2019-4-29 12:7:26 +0800 2018-2-13 17:2 ...
- 用C#简单实现的36进制转换代码
private const string initValue = "A0000001"; private static string cs = "0123456789AB ...
- 安装percona-toolkit.rpm时候报错:perl(Time::HiRes) is needed by percona-toolkit-2.2.16-1.noarch
1.安装percona-toolkit.rpm时候报错: warning: percona-toolkit.rpm: Header V4 DSA/SHA1 Signature, key ID cd2e ...
- Java ----单个list 删除元素
转载:https://www.cnblogs.com/lostyears/p/8809336.html 方式一:使用Iterator的remove()方法 public class Test { pu ...
- The chance for love doesn't come around every day.
The chance for love doesn't come around every day.爱的机会不是每天都有的.
- delphi 判断两个时间差是否在一个指定范围内
WithinPastYears.WithinPastMonths.WithinPastWeeks.WithinPastDays ... 判断两个时间差是否在一个指定范围内DateUtils.Withi ...