[Leetcode][Python]50: Pow(x, n)
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 50: Pow(x, n)
https://leetcode.com/problems/powx-n/ Implement pow(x, n). === Comments by Dabay===
技巧在于用x的平方来让n减半。
同时注意n为负数的情况,以及n为奇数的情况。
''' class Solution:
# @param x, a float
# @param n, a integer
# @return a float
def pow(self, x, n):
if x == 0:
return 0
elif n < 0:
return 1.0 / self.pow(x, -n)
elif n == 0:
return 1
elif n == 1:
return x
elif n % 2:
return self.pow(x*x, n/2) * x
else:
return self.pow(x*x, n/2) def main():
sol = Solution()
print sol.pow(0.00001, 2147483647) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python]50: Pow(x, n)的更多相关文章
- 【LeetCode】50. Pow(x, n) 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 递归 迭代 日期 题目地址: https://le ...
- 【一天一道LeetCode】#50. Pow(x, n)
一天一道LeetCode系列 (一)题目 Implement pow(x, n). (二)解题 题目很简单,实现x的n次方. /* 需要注意一下几点: 1.n==0时,返回值为1 2.x==1时,返回 ...
- [Leetcode]50. Pow(x, n)
Implement pow(x, n). 我的做法就比较傻了.排除了所有的特殊情况(而且double一般不可以直接判断==),然后常规情况用循环来做.- -||| 直接用循环,时间复杂度就比较大.应该 ...
- 【LeetCode】50. Pow(x, n) (3 solutions)
Pow(x, n) Implement pow(x, n). 按照定义做的O(n)肯定是TLE的. 利用这个信息:x2n = (xn)2 有个注意点,当n为负是,直接取反是不可行的. 由于int的表示 ...
- 【Leetcode】50. Pow(x, n)
Implement pow(x, n). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 O ...
- [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) 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设计模式--行为型模式--中介者模式
怎么理解中介者模式,我姑且用房产中介来理解吧.呵呵 中介者模式: 中介者模式 概述 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之 ...
- The type R is already defined 错误解决办法
今天在导入一个开源项目的时候遇到了The type R is already defined的错误,试过了删除R,clear project都还是报这个错,Google一下之后找到了解决办法在 Pro ...
- java 防止sql注入的方法(非原创)
一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQ ...
- MongoDB基本命令随便敲敲
1,mongoDB状态,版本,当前连接的数据库名称
- DWR常用<init-param>参数
1 安全参数 allowGetForSafariButMakeForgeryEasier 开始版本:2.0 默认值:false 描述:设置成true使DWR工作在Safari 1.x , 会稍微降低安 ...
- 用Python实现九九乘法表
1.用“#”组成的矩形的实现 代码 eight = int(input("Height:")) #用户输入高度 width = int(input("Width:&quo ...
- java的wait和notifyAll使用方法
class Num { private int num; public int getNum() { return num; } public void setNum(int num) { this. ...
- java学习笔记day07
1.throwable下面的子类分为两大类:Error 和 Exception 2.如果方法上有throws Exception,则必须对异常进行处理: try{ 需要检测异常代码 } ...
- Java - 注解 (Annotation)
Java - 注解 (Annotation) 一.基本的 Annotation > 使用 Annotation 时要在其前面增加 @符号,并把该 Annotation 当成一个修饰符 ...
- Illustrated C#学习笔记(一)
迄今为止最容易看懂的一本C#入门图书,的确是,很不错的一本书,继续读下去,并做好相关笔记吧. Chapter 1 C#和.NET框架 主要讲述了一些.NET框架下的一些不明觉厉的名词如CLR,CLI. ...