鱼C论坛

 找回密码
 立即注册
查看: 1587|回复: 0

[学习笔记] leetcode 50. Pow(x, n) 谷歌面试题

[复制链接]
发表于 2019-8-17 14:19:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Seawolf 于 2019-8-17 14:47 编辑
  1. Implement pow(x, n), which calculates x raised to the power n (xn).

  2. Example 1:

  3. Input: 2.00000, 10
  4. Output: 1024.00000
  5. Example 2:

  6. Input: 2.10000, 3
  7. Output: 9.26100
  8. Example 3:

  9. Input: 2.00000, -2
  10. Output: 0.25000
  11. Explanation: 2-2 = 1/22 = 1/4 = 0.25
  12. Note:

  13. -100.0 < x < 100.0
  14. n is a 32-bit signed integer, within the range [&#8722;231, 231 &#8722; 1]
复制代码


  1. class Solution {
  2.     public double myPow(double x, int n) {
  3.         
  4.             double result = 1.0;
  5.            
  6.             while(n != 0) {
  7.         
  8.         if(n > 0){
  9.             
  10.             result = x * result;
  11.             
  12.             n = n-1;
  13.         }
  14.         
  15.         else{
  16.             
  17.                 double a = Math.abs(x);
  18.                
  19.             result =  (1/x) * result;
  20.             
  21.             n = n + 1;
  22.         }
  23.         
  24.             }
  25.            
  26.             return result;
  27.         
  28.     }
  29. }
复制代码



这个code的问题是当n 很大的时候(十亿), 系统会 run out of time, 要解决这个问题就要引入 divide and conquer.


  1. class Solution {
  2.     public double myPow(double x, int n) {
  3.         //1.边界条件 x ^ 0 = 1
  4.         if(n == 0)
  5.             return 1;
  6.         
  7.         //2.折半拆分为子任务,子任务黑盒递归:2边子任务一样,只要算一边
  8.         //当n = Integer.MIN_VALUE的时候要特殊处理
  9.         //因为整形范围是-2^31到2^31 -1,所以或者我们使用long来存转换的数,或者特殊判断一下
  10.         double halfPow;
  11.         if(n < 0){
  12.             if (n == Integer.MIN_VALUE) {
  13.                 // return 1.0 / (myPow(x,-(Integer.MIN_VALUE+1)) * x);
  14.                 return 1.0 / (myPow(x, Integer.MAX_VALUE) * x);
  15.             }
  16.             return 1.0 / myPow(x, -n);
  17.         }
  18.         halfPow = myPow(x, n / 2);

  19.         
  20.         //3.返回值任务:把halfPow拼成fullPow
  21.         //要先偶后奇,不然报错
  22.         //偶
  23.         double res;
  24.         if(n % 2 == 0){
  25.             res = halfPow * halfPow ;
  26.         //奇
  27.         }else{
  28.             res = halfPow * halfPow * x;
  29.         }
  30.         
  31.         //4.返回
  32.         return res;
  33.     }
  34. }
复制代码


什么是Integer.MAX_VALUE 和 Integer.MIN_VALUE呢?

  1. Integer的MIN_VALUE
  2. 在JDK中,整型类型是有范围的  -2147483648~2147483647  ( -2^31 --- 2^31-1)
  3. 最大值为Integer.MAX_VALUE,即2147483647,最小值为Integer.MIN_VALUE -2147483648。
  4. 对整形最大值加1,2147483648(越界了),那么此时值为多少呢?结果是-2147483648,即是Integer.MIN_VALUE。
  5. 类似的,对Integer.MIN_VALUE取反或者取绝对值呢?仍为Integer.MIN_VALUE,因为值为-2147483648,绝对值2147483648超过Integer.MAX_VALUE 2147483647。
  6. 所以就有以下结果
  7. Integer.MAX_VALUE + 1 = Integer.MIN_VALUE
  8. //返回 Integer 值的绝对值。
  9. Math.abs(Integer.MIN_VALUE) =  Integer.MIN_VALUE
  10. Long,short,byte的结论是相同的。
  11. 这个语句的输出结果:
  12. System.out.println(1<<31== Integer.MIN_VALUE);//true
复制代码


16.jpg

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 01:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表