[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 ...
随机推荐
- C# 调用FFmpeg 根据图片合成视频
1.项目结构: 2.代码: using System; using System.Collections.Generic; using System.Diagnostics; using System ...
- eclipse下使用hibernate tools实现hibernate逆向工程
一 安装hibernate tools插件 1 在线安装 通过Eclipse的Help->Install New Software 在线安装插件,插件连接为: eclipse helios(3 ...
- 2015第10周五CSS—2
经常遇到的浏览器兼容性有哪些?如何解决? 1.浏览器默认的margin和padding不同.解决方案是加一个全局的*{margin:0;padding:0;}来统一. 2.IE6双边距bug:块属性标 ...
- 用于下载AD官网登录账号:User name: fuxin918@fuxin918.com Passeword: s6c0W1w8
用于下载AD官网登录账号:User name: fuxin918@fuxin918.com Passeword: s6c0W1w8
- java使用poi创建excel文件
import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRow;import or ...
- LeeCode-Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- HDU4762(JAVA大数)
Cut the Cake Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 【转】Android LCD(三):Samsung LCD接口篇
关键词:android LCD控制器 Framebuffer PWM 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台:samsung exynos ...
- 产生冠军(set,map,拓扑结构三种方法)
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- CF 338 D GCD Table(CRT)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 给定一个序列,a[1 ..k],问是否存在(i , ...