Implement pow(xn), which calculates x raised to the power n (x,n).

Example 1:                 Input: 2.00000, 10                 Output: 1024.00000

Example 2:                 Input: 2.10000,  3                  Output: 9.26100

Example 3:                 Input: 2.00000, -2                 Output: 0.25000                                 Explanation: 2-2 = 1/22 = 1/4 = 0.25

Note:

  • -100.0 < x < 100.0
  • n is a 32-bit signed integer, within the range [−231, 231 − 1]

思路: 这道题并不难,主要是情况考虑周全,例如 n为负数, x为0,1时等等问题。

   使用最简单的方法就是逐个累乘, 但是这种方法会在 n值非常大时,运行时间太长,导致超时。并且效率低。

     另外一种使用 这样一种公式来解决: an= an/2 + an/2 (当n为偶数时), an= (an/2 + an/2)a (当n为奇数时),这样计算效率提升。 时间复杂度为O(logn), 空间复杂度为O(1)。

  

 class Solution(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if x == 1.0 or x == 0.0: # 特殊情况的判断
return x if x == 1.0 else 0.0
neg_flag = False # 指数为负数的标志位
if n < :
neg_flag = True
n = abs(n) # 将负指数替换为正整数
res =
while n:
if n&: # 对于指数奇偶数的判断。
res *= x
x *= x              # x = x * x
n = n >> # 指数除以2
if neg_flag:
return /res
return res

【LeetCode每天一题】Pow(x, n)(平方)的更多相关文章

  1. 【JavaScript】Leetcode每日一题-平方数之和

    [JavaScript]Leetcode每日一题-平方数之和 [题目描述] 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例1: 输入:c = 5 ...

  2. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  3. leetcode第37题--Count and Say

    题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...

  4. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  5. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  6. LeetCode的刷题利器(伪装到老板都无法diss你没有工作)

    在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...

  7. LeetCode每天一题之两数之和

    这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...

  8. leetcode第三题

    leetcode第三题: 题目: 给定一个字符串,找出不含有重复字符的最长子串的长度. 源码(使用java语言): class Solution { public int lengthOfLonges ...

  9. [LeetCode] 系统刷题5_Dynamic Programming

    Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题 ...

  10. LeetCode 第 342 题(Power of Four)

    LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether ...

随机推荐

  1. fyzcms---相关文章推荐功能

    在用我写的fyzcms的做优化的时候,考虑到文章之间的权重集中,以及相互文章间的低耦合,所以设计了一个相关文章推荐的功能. 具体使用: <if condition="count($fi ...

  2. Python 读、写、追加csv文件详细以及注意事项

    一.利用csv库创建文件 首先导入csv文件 import csv 根据指定的path创建文件: def create_csv(path): with open(path, "w+" ...

  3. maven安装与创建多模块项目

    最新版已同步至 http://yywang.info/2014/05/31/maven-install-and-create-project/ maven是一个比较流行的项目管理工具,在最近参与的项目 ...

  4. SSH框架下的表单重复提交

    前几天做了一个功能,是在某个操作后,刷新父页面的,刷新时弹出了下面图的框: 网上查了之后发现这个框是表单重复提交时出现的.分析后发现,这个页面的上一个动作是form submit(在ssh框架下),这 ...

  5. java8 集合对象间的处理

    eg1:List<CarVo> carVoList = carService.getList(carVo); List<String> listVins = carVoList ...

  6. 160道Java技术面试题

    1.面向对象的特征有哪些方面?2.访问修饰符public,private,protected,以及不写(默认)时的区别?3.String 是最基本的数据类型吗?4.float f=3.4;是否正确?5 ...

  7. #Python学习笔记:1-3章 (基于《python编程,从入门到实践)

    第1-3章 这个文档是记录我学习python时一些学习笔记以及一些想法也可以称作复习笔记 第一章:起步这一章主要是从第一个"hello world"程序到python环境的搭建与配 ...

  8. 20165311 预备作业3 Linux安装及学习

    Linux安装 由于回家没有带笔记本,所以把VirtualBox安装在家里的台式上,回学校之后再重新在自己的笔记本上安装虚拟机.参考<基于VirtualBox安装Ubuntu图文教程>,整 ...

  9. ubuntu下安装bin文件

    从Java官网下载的安装文件,有的只有bin文件,没有.tar.gz文件. ①进入设备终端,通过sudo -s或su回车,切换到管理员用户:②输入管理员密码然后回车:③输入sudo chmod +x ...

  10. 类中的函数带有self,不带self的区别

    1.类里函数不带self,这是我们调用类里的函数直接用类名.函数名() class shop(object): def scan_goods(): #括号内不带self print('浏览商品') d ...