leetCode 50.Pow(x, n) (x的n次方) 解题思路和方法

Pow(x, n)

Implement pow(x, n).

思路:题目不算难,但是需要考虑的情况比较多。

具体代码如下:

public class Solution {
    public double myPow(double x, int n) {
    	boolean isMin0 = true;//结果负号
    	if(x > 0 || (n&1) == 0){//x>0或n为偶数
    		isMin0 = false;//为正
    	}
    	x = x < 0 ? -x:x;//将x统一设为正值
        double d = 1.0;
    	if(n > 0){//n分三种情况
            while(n > 0){
                d = d*x;
                n--;
            	if((Math.abs(d - 1.0) < 1e-15)){
            		d = 1.0;//默认等于1
            		break;
            	}else if((Math.abs(d) < 1e-15)){
            		d = 0;//默认等于0
            		break;
            	}
            }
    	}else if(n == 0){
    		return 1;
    	}else{
    		while(n < 0){
    			d = d/x;
    			n++;
            	if((Math.abs(d - 1.0) < 1e-15)){
            		d = 1;
            		break;
            	}else if((Math.abs(d) < 1e-15)){
            		if(isMin0){//除数等于0,将等于正负无穷
            			return Double.MIN_VALUE;
            		}else{
            			return Double.MAX_VALUE;
            		}
            	}
    		}
    	}
		return isMin0?-d:d;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 05:11:08

leetCode 50.Pow(x, n) (x的n次方) 解题思路和方法的相关文章

leetCode 21.Merge Two Sorted Lists (合并排序链表) 解题思路和方法

Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 思路:对两个已排序的单链表合并.算法上比较简单,与归并排序类似.只是数据结构上以前学的,现在忘的比较多,写第一遍的时候比较费力.而且想把重复代码写出方法,但是方法怎么都不

leetCode 29.Divide Two Integers (两整数相除) 解题思路和方法

Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路:这个题算法上不是很难,但是通过率相当低,只有15%,果然,自己在写完之后,各种出错,而且错误不是算法上的错误,是各种边界值没有考虑到,很多溢出错误等.下面是具体代码,有详细注释. public class Solution { p

leetCode 32.Longest Valid Parentheses (有效的最大括号) 解题思路和方法

Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length

leetCode 71.Simplify Path(化简路径) 解题思路和方法

Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider

leetCode 92.Reverse Linked List II (反转链表II) 解题思路和方法

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

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#50 Pow(x, n)

Just... Implement pow(x, n). Solution: 1)Naive solution:multiply x by itself for n-1 times. (Or simply reyurn 1 if n==0). This takes O(n) time. When n is big enough, it's relatively slow. 2)Fast Power. Take pow(3, 11) as an example. Here's what we're

[LeetCode] 50. Pow(x, n) Java

题目: Implement pow(x, n). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 Output: 9.26100 题意及分析:实现求x的n次方,使用分治法,复杂度降低到log2n 代码: public class Solution { public double myPow(double x, int n) { if(n < 0) return 1/pow(x,-n); e

leetcode 50 pow(x,y)

Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 Output: 9.26100 Example 3: Input: 2.00000, -2 Output: 0.25000 Explanation: 2-2 = 1/22 = 1/4 = 0.25 Note: