【Leetcode】50. Pow(x, n)

Implement pow(x, n).

Example 1:

Input: 2.00000, 10  Output: 1024.00000

Example 2:

Input: 2.10000, 3   Output: 9.26100
package medium;

public class L50MyPow {
    // 调用Math.pow() 函数
    public double mypow(double x, int n) {
        double nn = n;
        return Math.pow(x, nn);
    }

    //本题就x的n次方,如求2的4次方。可以4个2相乘,也可以先两个2相乘,之后两个4在相乘。
    public double myPow(double x, int n) {
        //防止n越界2147483647,用long类型的N来代替n
        long N = n;
        double result = 1.0;
        // double类型的x 判断x是否为0
        if ((x - 0.0 < -0.000000000001) && n < 0) {
            return 0.0;
        }
        //指数小于零,求得的数是用负指数相反数求得的值的倒数。
        if (N < 0) {
            N = -N;
            x = 1 / x;
        }
        double current_product = x;
        for (long i = N; i > 0; i /= 2) {
            if ((i % 2) == 1) {
                result = result * current_product;
            }
            current_product = current_product * current_product;
        }
        return result;
    }

    public static void main(String[] args) {
        L50MyPow cc = new L50MyPow();
        //测试负数的整数次方。  4.0
        System.out.println(cc.myPow(-2, 2));
        System.out.println("!!!!!!!!!!!!!!!!!!!");
        //小数的负数次方   2.543114507074558E-5
        double re = cc.myPow(34.00515, -3);
        System.out.println(re);
        //小数的大正整数次方次方 0.0
        System.out.println(cc.myPow(0.00001, 2147483647));
    }
}

原文地址:https://www.cnblogs.com/yumiaomiao/p/8418420.html

时间: 2024-11-09 03:21:31

【Leetcode】50. Pow(x, n)的相关文章

【javascript】50. Pow(x, n)

50. Pow(x, n) javascript使用var声明数据变量,变量没有固定的类型,如果需要使用整数,使用parseInt()进行转换. 1 var myPow = function(x, n) { 2 if(n==0)return 1; 3 if(n<0){ 4 n=-n; 5 x=1/x; 6 } 7 return (n%2==0) ? myPow(x*x,n/2) : (x*myPow(x*x,parseInt(n/2))); 8 };

【一天一道LeetCode】#50. Pow(x, n)

一天一道LeetCode系列 (一)题目 Implement pow(x, n). (二)解题 题目很简单,实现x的n次方. /* 需要注意一下几点: 1.n==0时,返回值为1 2.x==1时,返回值为1:x==-1时,根据n的奇偶来判断 3.n==-2147483648,特殊情况,int的范围时-2147483648-2147483647, */ class Solution { public: double myPow(double x, int n) { if(n==0||x==1) r

【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】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. public class Solution { public String intToRoman(int num) { StringBuilder sb = new StringBuilder(); if(num==0) return sb.toString(); while(num

【Leetcode】Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n