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 ...
随机推荐
- 二.Python基础语法和数据类型
Python第二节 基础语法和数据类型 Python编码 python3默认情况下源码文件以UTF-8编码, 字符串均为unicode字符串.同时也可以通过# -*- coding: cp-1252 ...
- css3条纹进度条
新建div,取名progress,如下 <div class="progress"></div> 在里面插入条纹进度条,以及进度显示文本进度: <di ...
- usleep - 睡眠若干微秒
总览 (SYNOPSIS) usleep [number] 描述 (DESCRIPTION) usleep 睡眠 指定的 微秒数. 缺省值 是 1. 选项 (OPTIONS) --usage 显示 简 ...
- linux指令【参考鸟哥的Linux私房菜】
date指令 显示日期 cal指令 显示日历 bc 计算器 scale+number 显示几位小数 quit退出bc tab键 命令提示 入输入ca,按下tab键,会将所有ca的指令全部 ...
- Jenkins添加Windows自动化构建方案
一.为Jenkins添加Windows节点 这里需要填写远程工作目录,启动方法一项一定要选择"Launch agent via Java Web Start"一项,其它的保持默认. ...
- Java SE(1)
Java SE基础回顾 1.循环语句中的break是终止全部循环,跳出循环体:而continue是终止本次循环,跳执行下一循环 2.return语句有两个作用:返回值:结束方法的运行 3.对于java ...
- time 类
timeStamp = time.time() #获取当前的时间戳 print(timeStamp) # 1555555453.6283455 timeTuple = time.localtime(t ...
- mysql和postgresql查询数据库中哪些表包含某个字段
想知道数据库中哪表含有edu_status字段 mysql> select table_name,column_name from information_schema.columns wh ...
- js 浮点数计算问题
//说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显.这个函数返回较为精确的加法结果. //调用:jsAdd(arg1,arg2) //返回值:arg1加上arg2的精确 ...
- leetcode- 距离顺序排序矩阵单元格
C++解法: #include <iostream> #include <vector> #include <map> #include <algorithm ...