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 ...
随机推荐
- Linux 中 sqlite3 基本操作
https://www.runoob.com/sqlite/sqlite-commands.html 一 .linux 下安装数据库和创建一个数据库 1. Linux 下安装sqlite3 需要两个命 ...
- 用C#简单实现的36进制转换代码
private const string initValue = "A0000001"; private static string cs = "0123456789AB ...
- 数组去重Set也可实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- nodejs route的简单使用
demo var express=require('express'); var app=express(); var routeUser=express.Router(); var routeTea ...
- flink on yarn启动失败
我启动hadoop on yarn 集群后 [root@node1 flink-1.6.1]# ./bin/yarn-session.sh -n 2 -jm 1024 -tm 1024 报的如下错误 ...
- 解决 php artisan route:list 报错oauth-private.key文件不存在或不可读的
进入项目根目录命令行执行 php artisan passport:install 然后执行php artisan route:list,会提示 Class App\Http\Controllers\ ...
- 32. Random类
1.Random class Random implements java.io.Serializable 下面是一个简单验证码的代码: public static void main(String[ ...
- vue导出table内容至excel——转
一:在项目中需要安装2个依赖项,如下命令: npm install --save file-saver xlsx 二:在vue文件中如下使用即可: <template> <div c ...
- leetcode-126-单词接龙
题目描述: class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: list) -> list ...
- Batch - attrib 命令
原文地址:CMD中使用attrib命令设置文件只读.隐藏属性详解 本文介绍一个cmd下的一个attrib.exe的小程序,它可以用来设置文件的属性.我们知道文件的属性有只读.隐藏.系统.存档和无内容索 ...