Pow(x, n)

Implement pow(xn).

按照定义做的O(n)肯定是TLE的。

利用这个信息:x2n = (xn)2

有个注意点,当n为负是,直接取反是不可行的。

由于int的表示范围是[2-31, 231-1],当n为INT_MIN时,取反会溢出。

因此需要对n==INT_MIN单独考虑。

另外,除以2可以用右移1位来实现。

解法一:递归

  1. class Solution {
  2. public:
  3. double pow(double x, int n) {
  4. if(n < )
  5. {
  6. if(n == INT_MIN) //-INT_MIN will cause overflow
  7. return pow(x, n+)/x;
  8. else
  9. {
  10. x = /x;
  11. n = -n;
  12. }
  13. }
  14. return Helper(x, n);
  15. }
  16. double Helper(double x, int n)
  17. {//n > 0
  18. if(n == )
  19. return ;
  20. double partRes = Helper(x, n>>);
  21. if(n% == )
  22. return partRes*partRes*x;
  23. else
  24. return partRes*partRes;
  25. }
  26. };

解法二:非递归

对于n的二进制表示,考虑每一位的0/1。

举例n==5,二进制表示为101

右数第一位为1,需要乘以x

右数第二位为0,不需要乘以x2

右数第三位为1,需要乘以x4

  1. class Solution {
  2. public:
  3. double pow(double x, int n) {
  4. if(n == )
  5. return ;
  6.  
  7. int sign = ;
  8. if(n == INT_MIN)
  9. return pow(x, n+) / x;
  10. else if(n < )
  11. {
  12. sign *= -;
  13. n *= -;
  14. }
  15.  
  16. double ret = ;
  17. double mul = x;
  18. while(n)
  19. {
  20. if(n & )
  21. ret *= mul;
  22. n >>= ;
  23. mul *= mul;
  24. }
  25.  
  26. if(sign == )
  27. return ret;
  28. else
  29. return / ret;
  30. }
  31. };

解法三:just a joke

  1. class Solution {
  2. public:
  3. double pow(double x, int n) {
  4. return std::pow(x,n);
  5. }
  6. };

【LeetCode】50. Pow(x, n) (3 solutions)的更多相关文章

  1. 【LeetCode】50. Pow(x, n) 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 递归 迭代 日期 题目地址: https://le ...

  2. 【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 ...

  3. 【一天一道LeetCode】#50. Pow(x, n)

    一天一道LeetCode系列 (一)题目 Implement pow(x, n). (二)解题 题目很简单,实现x的n次方. /* 需要注意一下几点: 1.n==0时,返回值为1 2.x==1时,返回 ...

  4. 【LEETCODE】50、数组分类,简单级别,题目:888,1013,896,485,448,697

    package y2019.Algorithm.array; import java.util.HashSet; import java.util.Set; /** * @ProjectName: c ...

  5. 【LeetCode】12. Integer to Roman (2 solutions)

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  6. 【LeetCode】234. Palindrome Linked List (2 solutions)

    Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could ...

  7. 【LeetCode】206. Reverse Linked List (2 solutions)

    Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can ...

  8. 【LeetCode】200. Number of Islands (2 solutions)

    Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...

  9. 【LeetCode】40. Combination Sum II (2 solutions)

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

随机推荐

  1. Python3.6学习笔记(六)

    WSGI Python Web Server Gateway Interface 规范学习 由于Python的灵活性,提供了多种方式可以作为服务端语言,包括Python编写的服务器(Medusa).P ...

  2. Coursera课程python中的一些程序

    Index of /code Name Last modified Size Description Parent Directory - BeautifulSoup.py 07-Aug-2015 1 ...

  3. 混沌数学之拉比诺维奇-法布里康特方程(Rabinovich-Fabrikant equations)

    拉比诺维奇-法布里康特方程(Rabinovich-Fabrikant equations)是 1979年苏联物理学家拉比诺维奇和法布里康特提出模拟非平衡介 质自激波动的非线性常微分方程组: dot{x ...

  4. 第九章 LinkedBlockingQueue源码解析

    1.对于LinkedBlockingQueue需要掌握以下几点 创建 入队(添加元素) 出队(删除元素) 2.创建 Node节点内部类与LinkedBlockingQueue的一些属性 static ...

  5. 使用gitolite进行git服务器搭建

    使用gitolite进行git服务器搭建 https://blog.csdn.net/pan0755/article/details/78460941 使用gitolite搭建,然后需要有个客户端进行 ...

  6. [INS-30131]执行安装程序验证所需的初始设置失败(原因:无法访问临时位置)

    [INS-30131]执行安装程序验证所需的初始设置失败(原因:无法访问临时位置) 学习了:https://blog.csdn.net/killvoon/article/details/5182192 ...

  7. FrameWork逆向工程之MotioPI

    在BI项目建设的过程中我们一般都是有备份的,而且这个是必须有的!特别是例如ETL Model,还有Data Model这一类的元数据,这些东西如果我们没有备份,而恰好的我们的开发模型又在某一天离我们而 ...

  8. Linux shell 脚本入门教程+实例

    原文:http://www.wiquan.com/article/136 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是shell仍然是一个非常灵活的工具.She ...

  9. WPF代码模板-布局部分

    Grid 两行和三列 <Grid ShowGridLines="True"> <Grid.RowDefinitions> <RowDefinition ...

  10. iOS用户体验之-导航之道

    iOS用户体验之-导航之道 用户不会意识到有导航指向的存在除非他遇到非预期的效果. 能够说导航时逻辑跳转的节点.所以导航对用户体验是至关重要的. iOS中有三种类型的导航.每一种适合不同类型的app. ...