[LeetCode][JavaScript]Pow(x, n)

Pow(x, n)

Implement pow(xn).

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



注意x和n都可能是负数。

递归求解,比如求3的4次方,可以拆成3的2次方相乘;3的5次就是3^2相乘再乘2。

 1 /**
 2  * @param {number} x
 3  * @param {number} n
 4  * @return {number}
 5  */
 6 var myPow = function(x, n) {
 7     if(n >= 0){
 8         return pow(Math.abs(n));
 9     }else{
10         return 1 / pow(Math.abs(n));
11     }
12
13     function pow(n){
14         var temp = 0;
15         if(n === 0){
16             return 1;
17         }else if(n === 1){
18             return x;
19         }else if(n % 2 === 1){
20             temp = pow((n - 1) / 2);
21             return temp * temp * x;
22         }else if(n % 2 === 0){
23             temp = pow(n / 2);
24             return temp * temp;
25         }
26     }
27 };
时间: 2024-10-13 22:23:14

[LeetCode][JavaScript]Pow(x, n)的相关文章

[LeetCode][JavaScript]Pascal's Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] https://leetcode.com/problems/pascals-triangle/ 杨辉三角. 每行第一个和最后一个是1,其余是pre[i - 1] +

LeetCode:Pow(x, n)

1.题目名称 Pow(x, n)(求指定数字x的整数次幂) 2.题目地址 https://leetcode.com/problems/powx-n/ 3.题目内容 英文:Implement pow(x, n) 中文:给定底数x和指数n,求x的n次幂 4.解题方法1 在Java中,有一个偷懒的办法是这样实现的: /**  * 功能说明:LeetCode 50 - Pow(x, n)   * 开发人员:Tsybius2014  * 开发时间:2015年8月8日  */ public class So

【LeetCode】Pow(x, n)

Implement pow(x, n). 思路:快速幂运算,需要考虑指数为负数,同时底数为0的情况,这种属于异常数据,代码里没有体现. class Solution { public: double pow_abs(double x, unsigned int n) { if (n == 0) { return 1; } if (n == 1) { return x; } double res = pow(x, n>>1); res *= res; if (n & 0x1 == 1)

LeetCode 50 Pow(x, n)(Math、Binary Search)(*)

翻译 实现pow(x, n). 原文 Implement pow(x, n). 分析 首先给大家推荐维基百科: zh.wikipedia.org/wiki/二元搜尋樹 en.wikipedia.org/wiki/Binary_search_tree 其次,大家也可以看看类似的一道题: LeetCode 69 Sqrt(x)(Math.Binary Search)(*) 然而这题我还是没有解出来,看看别人的解法-- class Solution { private: double myPowHel

[LeetCode][JavaScript]Reverse Integer

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

[LeetCode][JavaScript]Gray Code

Gray Code The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin

[LeetCode][JavaScript]Valid Sudoku

https://leetcode.com/problems/valid-sudoku/ Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sud

[LeetCode]75. Pow(x,n)幂运算

Implement pow(x, n). Subscribe to see which companies asked this question 解法1:最简单的即是n个x直接相乘,毫无疑问会超时Time Limit Exceeded class Solution { public: double myPow(double x, int n) { if(x < 0.000001) return 0; if(n == 0) return 1; if(n < 0) return 1.0 / my

LeetCode 之 Pow(x, n)(分治法)

[问题描述] Implement pow(x, n). 1.[基础知识] 1)分治的意识,一道O(N)的算法题,琢磨出O(lgN)的思想出来就是要求: 2.[屌丝代码] 卡壳的地方: 1.Time Limit Exceeded. #include <vector> #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace