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, 禁止轉載 🈲️,侵權必究⚠️!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章