• LeetCode Pow(x, n)算法题解 All In One


    LeetCode Pow(x, n)算法题解 All In One

    js / ts 实现 Pow(x, n)

    
    ## 实现原理 图解
    
    > 数学知识
    
    ```js
    Math.pow(n, 0)
    // 任何数 n 的 0 次幂都等于 1;
    Math.pow(0, 0)
    // 0 的 0 次幂等于 1;
    
    Math.pow(0, m)
    // 0 的 任何数 m > 0 次幂都等于 0;
    
    Math.pow(0, m)
    // 0 的 任何数 m < 0 次幂都等于 Infinity;
    
    
    Math.pow(n, m)
    // 任何数 n 的 m 次幂都等于 m 个累乘;
    
    Math.pow(n, -m)
    // 任何数 n 的 -m 次幂都等于 (1 除  n 的 m 次幂) 1 / n**m ;
    
    
    
    /*
    
    Math.pow(-1, 0)
    1
    Math.pow(0, 0)
    1
    Math.pow(1, 0)
    1
    
    Math.pow(0, 3);
    0
    Math.pow(0, 0.5);
    0
    
    Math.pow(0, -1);
    Infinity
    Math.pow(0, -0.5);
    Infinity
    
    
    Math.pow(3, 3);
    27
    Math.pow(2, 0.5);
    1.4142135623730951
    
    Math.pow(2, -2);
    0.25
    Math.pow(2, -4);
    0.0625
    
    
    Math.pow(-2, 0.5);
    NaN
    Math.pow(-2, 0);
    1
    Math.pow(-2, 1);
    -2
    Math.pow(-2, 2);
    4
    Math.pow(-2, 3);
    -8
    
    */
    
    function myPow(x: number, n: number): number {
      // 超出时间限制 ❌
      // function pow(num, times) {
      //   let sum = 1;
      //   while(times > 0) {
      //     sum *= num;
      //     times--;
      //   }
      //   return sum;
      // }
      // 超出时间限制 ❌
      // function pow(num, times) {
      //   let sum = 1;
      //   for(let i = 0; i < times; i++) {
      //     sum = sum * num;
      //   }
      //   return sum;
      // }
      let result: number;
      if(n === 0) {
        result = 1;
      } else if(n < 0) {
        result = 1 / (x ** Math.abs(n));
        // result = 1 / pow(x, Math.abs(n));
      } else {
        result = x ** n;
        // result = pow(x, n);
      }
      return result;
    };
    

    50. Pow(x, n)

    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     * @created 2022-08-12
     * @modified
     *
     * @description 50. Pow(x, n)
     * @description 50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的整数 n 次幂函数(即,x^n )。
     * @difficulty Medium
     * @ime_complexity O(n)
     * @space_complexity O(n)
     * @augments
     * @example
     * @link https://leetcode.com/problems/powx-n/
     * @link https://leetcode-cn.com/problems/powx-n/
     * @solutions
     *
     * @best_solutions
     *
     */
    
    // export {};
    
    const log = console.log;
    
    // function myPow(x: number, n: number): number {
    //   // 超出时间限制 ❌
    //   // function pow(num, times) {
    //   //   let sum = 1;
    //   //   while(times > 0) {
    //   //     sum *= num;
    //   //     times--;
    //   //   }
    //   //   return sum;
    //   // }
    //   // 超出时间限制 ❌
    //   // function pow(num, times) {
    //   //   let sum = 1;
    //   //   for(let i = 0; i < times; i++) {
    //   //     sum = sum * num;
    //   //   }
    //   //   return sum;
    //   // }
    //   // 位运算 ??? 负数
    //   let result: number;
    //   if(n === 0) {
    //     result = 1;
    //   } else if(n < 0) {
    //     result = 1 / (x ** Math.abs(n));
    //     // result = 1 / pow(x, Math.abs(n));
    //   } else {
    //     result = x ** n;
    //     // result = pow(x, n);
    //   }
    //   // return result;
    //   return result;
    // };
    
    // failed ❌ 9.261000000000001, 本地测试
    function myPow(x: number, n: number): string {
      let result: number;
      if(n === 0) {
        result = 1;
      } else if(n < 0) {
        result = 1 / (x ** Math.abs(n));
        // result = 1 / pow(x, Math.abs(n));
      } else {
        result = x ** n;
        // result = pow(x, n);
      }
      // return result;
      return result.toFixed(5);
    };
    
    // 测试用例 test cases
    // const testCases = [
    //   {
    //     inputs: [2.00000, 10],
    //     result: 1024.00000,
    //     desc: 'value equal to 1024.00000',
    //   },
    //   {
    //     inputs: [2.10000, 3],
    //     result: 9.26100,
    //     desc: 'value equal to 9.26100',
    //   },
    //   {
    //     inputs: [2.00000, -2],
    //     result: 0.25000,
    //     desc: 'value equal to 0.25000',
    //   },
    //   {
    //     inputs: [2.00000, -2147483648],
    //     result: 0.00000,
    //     desc: 'value equal to 0.00000',
    //   },
    // ];
    const testCases = [
      {
        inputs: [2.00000, 10],
        result: '1024.00000',
        desc: 'value equal to 1024.00000',
      },
      {
        inputs: [2.10000, 3],
        result: '9.26100',
        desc: 'value equal to 9.26100',
      },
      {
        inputs: [2.00000, -2],
        result: '0.25000',
        desc: 'value equal to 0.25000',
      },
      {
        inputs: [2.00000, -2147483648],
        result: '0.00000',
        desc: 'value equal to 0.00000',
      },
    ];
    
    for (const [i, testCase] of testCases.entries()) {
      const [first, second] = testCase.inputs;
      const result = myPow(first, second);
      log(`test case i result: \n`, result === testCase.result ? `passed ✅` : `failed ❌`, result);
      // log(`test case i =`, testCase);
    }
    
    
    
    /* 
    
    测试用例
    
    2.00000
    10
    2.10000
    3
    2.00000
    -2
    
    // 超出时间限制 ❌
    2.00000
    -2147483648
    
     */
    
    
    /*
    
    Math.pow(-1, 0)
    1
    Math.pow(0, 0)
    1
    
    Math.pow(0, 3);
    0
    Math.pow(0, 0.5);
    0
    
    Math.pow(0, -1);
    Infinity
    Math.pow(0, -0.5);
    Infinity
    
    
    Math.pow(3, 3);
    27
    Math.pow(2, 0.5);
    1.4142135623730951
    
    Math.pow(2, -2);
    0.25
    Math.pow(2, -4);
    0.0625
    
    
    Math.pow(-2, 0.5);
    NaN
    Math.pow(-2, 0);
    1
    Math.pow(-2, 1);
    -2
    Math.pow(-2, 2);
    4
    Math.pow(-2, 3);
    -8
    
    */
    
    

    https://leetcode.cn/problems/powx-n/

    leetcode 题解 / LeetCode Solutions

    https://www.youtube.com/results?search_query=+Leetcode+50

    https://www.youtube.com/playlist?list=PLamwFu9yMruCBtS2tHUD77oI_Wsce-syE

    https://www.youtube.com/channel/UCftIXZeipv4MTVwmfFphtYw/videos

    https://neetcode.io/
    https://leetcode.com/problems/powx-n/

    类似问题

    refs



    ©xgqfrms 2012-2020

    www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

    原创文章,版权所有©️xgqfrms, 禁止转载 ️,侵权必究⚠️!


  • 相关阅读:
    jquery--blur()事件,在页面加载时自动获取焦点
    jquery三级联动
    工具集
    兼容各个浏览器:禁止鼠标选择文字事件
    jquery 事件委托(利用冒泡)
    小功能1:多种方法实现网页加载进度条
    JavaSE| 泛型
    SSM整合
    Redis数据库 02事务| 持久化| 主从复制| 集群
    Hadoop| MapperReduce02 框架原理
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/16584675.html
Copyright © 2020-2023  润新知