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 Solution {
    
    /**
     * 计算x的n次幂
     * @param x 底数
     * @param n 指数
     * @return 幂
     */
    public double myPow(double x, int n) {
        return Math.pow(x, n);
    }
}

5、解题方法2

由于本题输入是一个整型,所以只需要考虑n为整数的情况。如果不采用函数Math.pow,就需要针对n大于0和n小于0做分类讨论,并使用递归的方法减少重复乘法的运算。

/**
 * 功能说明:LeetCode 50 - Pow(x, n) 
 * 开发人员:Tsybius2014
 * 开发时间:2015年8月8日
 */
public class Solution {
    
    /**
     * 计算x的n次幂
     * @param x 底数
     * @param n 指数
     * @return 幂
     */
    public double myPow(double x, int n) {
        
        //特例,快速给出结果
        if (n == 0) {
            return 1;
        } else if (n == 1) {
            return x;
        } else if (n == -1) {
            return 1 / x;
        }
        
        //要考虑n为正数和负数两种情况
        double temp;
        if (n > 0) {
            temp = myPow(x, n / 2);
            if (n % 2 != 0) {
                return temp * temp * x;
            } else {
                return temp * temp;
            }
        } else {
            temp = 1 / myPow(x, -n / 2);
            if (n % 2 != 0) {
                return temp * temp / x;
            } else {
                return temp * temp;
            }
        }
    }
}

END

时间: 2024-08-01 10:32:57

LeetCode:Pow(x, n)的相关文章

【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]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 050 Pow(x, n)

题目要求:Pow(x, n) Implement pow(x, n). 代码如下: class Solution { public: //采用二分法 //时间复杂度 O(logn),空间复杂度 O(1) double pow(double x, int n) { //要考虑n < 0的情况! if(n < 0) return 1.0 / power(x, -n); else return power(x, n); } double power(double x, int n){ if(n ==

LeetCode:Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 动态规划解法 T(n) = O(n^2)  ,S(n) = O(n^2); Solutio

leetcode:Pascal&amp;#39;s Triangle

一.     题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二.     分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数的和值 须要注意的是,当行数为0时输出[[1]] 结果为一个二维数组,所以不难想到解决方式. 每层保存前一行的指针,然后当前行数据依据上一行来得到,每一个元素就是上一行两个相邻元素相加(第一个和最后一个元素是1). 算法时间复杂度应该是O(1+2+3+...+n)=O(n^2),空间上仅仅须要二维数

LeetCode: Triangle 题解

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i

LeetCode: Permutations II 题解

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

LeetCode:(Array-189) Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

每日算法之三十九:Pow(x, n)

实现浮点类型的幂运算,函数原型为: double pow(double x, int n) 在求解这个问题的时候是一个很挣扎的过程,因为它不是报错而是一直提示你超出时间,那么必须一次次的考虑怎样降低时间复杂度. 首先最直接的思路是下面这样的,就跟直观的数学求解一样. double pow(double x, int n) { if(n==0) return 1.0; if(n<0) return 1.0/pow(x,-n); return x*pow(x,n-1); } 但是会提示你超出时间,这