Leetcode | | 50. Pow (x, N)

Source: Internet
Author: User

Problem:

Implement Pow (x, n).

Hide TagsMath Binary SearchTest instructions: To find the n power of X

Thinking:

(1) The simplest thought is the intuitive mathematical power function to find the method, the test passed. Algorithm time complexity of O (n)

(2) Follow the label prompts, using the binary search method. Pow (x,n) = POW (X,N-N/2) *pow (X,N/2), each scaling the size of n, attention to the parity of N is discussed, the algorithm time complexity is log (n)

(3) In addition to the above method, a very ingenious and fast method is also mentioned here, which is described as follows:

Consider the binary representation of N. For example, if it was "10001011", then X^n = x^ (1+2+8+128) = x^1 * x^2 * x^8 * x^128. Thus, we don ' t want to loop n times to calculate x^n. We loop through each bit, and if the i-th bit is 1 and then we add x^ (1 << i) to the result. Since (1 << i) is a power of 2, x^ (1<< (i+1)) = Square (x^ (1<<i)). The loop executes for a maximum of log (n) times.

The method calculates the power of x by scanning 1 of different positions in the binary representation of N, with the worst being O (n), but with a good average complexity

Code

(1) Recursive method: Accepted

Class Solution {public:    double pow (double x, int n) {        double ret=1.0;        if (x==1.0)            return 1.0;        if (x==-1.0)        {            if (n%2==0)                return 1.0;            else                return-1.0;        }        if (n<0)            return 1.0/pow (x,-n);        while (n)        {            if (ret==0)//Prevent run timeout                return 0;            ret*=x;            n--;        }        return ret;    }};


(2) Dichotomy: Accepted

Class Solution {public:    double pow (double x, int n) {        //double ret=1.0;        if (x==1.0)            return 1.0;        if (x==-1.0)        {            if (n%2==0)                return 1.0;            else                return-1.0;        }        if (n==0)            return 1.0;        if (n<0)            return 1.0/pow (x,-n);        Double Half=pow (x,n>>1);        if (n%2==0)            return half*half;          else            return x*half*half;}    };
(3)

In order to properly calculate the n power of X, there are a few things to consider:

1) x is 0 o'clock, the power of 0 is 1, and negative power is meaningless; judging whether x equals 0 cannot be used directly with "= =".

2) when n is Int_min,-n is not Int_max, this requires extra care.

3) Try to use shift operations instead of division operations to speed up the execution of the algorithm.

Class Solution {public:    double pow (double x, int n) {        //Start Typing your C + + solution below        //Do not writ e int main () function        if (n<0) {if (n==int_min) return 1.0/(POW (X,int_max) *x); Elsereturn 1.0/pow (x,-n);}        if (n==0)            return 1.0;double ans = 1.0; for (;n>0; x *= x, n>>=1) {if (n&1>0) ans *= x;} return ans;    };



Leetcode | | 50. Pow (x, N)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.