马上各种校招要开始了,怎么也得准备一下,之前一直在看看机器学习,NLP方面的东西,收获很多。最近换换脑子,回过头来做做leetcode,感觉还是蛮有意思的。今天刷了个水题,AC不高,然而难度也不高。。不知道为啥。第一次提交用了最最锉的方法,找虐的,很明显超时。于是开始想,第一个想到的就是二分,本来要做n次的计算,现在只要log(n)就可以了,没啥边界,注意n是非正数的情况就可以了:

class Solution:
# @param {float} x
# @param {integer} n
# @return {float}
def myPow(self, x, n):
res = 1
if n == 0:
#return 1
if n < 0:
x = float(1)/x
n = 0-n
while(n > 0):
if n % 2 == 1:
res *= x
n = n/2
x *= x
return res

  之后还想过是不是可以弄个3次方、4次方啥的,后来给AC了也就没整了。不过想想道理也是一样的,就是考虑不能整除的情况,相对来说,2次方考虑的较少,时间也算可以。不过上面非递归的情况理解起来有点困难的话,来个递归的版本:

class Solution:
# @param {float} x
# @param {integer} n
# @return {float}
def myPow(self, x, n):
if n < 0:
x = float(1)/x
n = 0-n if n == 0:
return 1
v = self.myPow(x, n/2)
if n % 2 == 0:
return v * v
else:
return v * v * x

【leetcode】Pow(x,n)的更多相关文章

  1. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  2. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  3. 【leetcode】688. Knight Probability in Chessboard

    题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. 一个div,包含三个小的div,平均分布的样式

    从11月份开始,自学前端开发,写静态页面中,经常用到一个大的div下包含三个小的div,平均分布div大小样式,写过多次,也多次忘记,每次都要现找资料,不想之后,在这么麻烦,索性今天自己记录一下,方便 ...

  2. ABP mapto 映射

    obj1.MapTo(obj2); obj1=>obj2: 在obj1实体里添加映射 [AutoMap(typeof(obj2))] public class obj1 { }

  3. 记录那些我不清楚的知识点(HTML)

    <div class="link"><a href="http://www.baidu.com/" target="iframeHt ...

  4. XUtils——GET请求

    //HttpUtils实例化对象     HttpUtils http = new HttpUtils();       /*                *发送请求send(HttpMethod ...

  5. 网络-->监控-->OID-->BGP

    说明:暂时发现只适合cisco设备,h3c的交换机只支持部分OID(支持版本.AS号.ROUTER-ID)

  6. ng-repeat && ng-options的故事

    ng-repeat && ng-options的故事   1. <select class="input-small" ng-model="newH ...

  7. Getting Started With Hazelcast 读书笔记(第四章)

    第四章 分而治之 在指导了如何进行基本使用之后,又再次进入理论模块. Hazelcast的基本策略就是切片分区,默认是271个片.内置一个 partition table记录那个节点是那个分区,并在h ...

  8. iOS TableView如何刷新指定的cell或section

    指定的section单独刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:indexPath.row]; [tableview relo ...

  9. linux系统编程之lseek帮助文档

    通过man 2 lseek可以查看linux中的系统函数lseek函数的帮助文档,为了更好的学习,我把这些重要内容翻译过来 NAME lseek - reposition read/write fil ...

  10. HttpWebRequest header configuration

    more details: http://www.cnblogs.com/yczz/archive/2012/06/01/2530484.html 在HttpWebRequest中,有一些header ...