[Leetcode]75. Pow (x,n) power operation

Source: Internet
Author: User

Implement Pow (x, n).

Subscribe to see which companies asked this question

Solution 1: The simplest is that n x is multiplied directly, without a doubt time Limit exceeded

classSolution { Public:    DoubleMypow (DoubleXintN) {if(X <0.000001)return 0; if(n = =0)return 1; if(N <0)return 1.0/MYPOW (x,-N); Doubleres =1.0;  for(inti =0; I < n; ++i) Res*=x; returnRes; }};

Solution 2: Consider POW (4,12), can be calculated as: Pow (4,12) =pow (4,8) *pow (bis), and POW (4,8) can be through POW (4,1)-->pow (4,2)-->pow ()-->pow ( 4,8) The same can be calculated by the POW (4,1)-->pow (4,2)-->pow (bis). That is, when calculating to a power pow (x,k), if the current power of the Square pow (x,2k) will not be greater than the result (judging by 2k<=n), then the direct square. So you can write the following code:

classSolution { Public:    DoubleMypow (DoubleXintN) {if(X <0.000001)return 0; if(n = =0)return 1; if(N <0)return 1.0/MYPOW (x,-N); Doubleres =1.0;  while(N >0) {            Doublef =x; intloop =1;  while(Loop <<1) <=N) {f*=F; Loop<<=1; } N-=Loop; Res*=F; }        returnRes; }};

This reduces the number of operations to some extent, but as the above example shows, there are still a lot of redundant computations. Also time Limit exceeded is timed out after Leetcode is committed . You can use map<double,int> to record a calculated power value, and all values after the first loop can be obtained by looking up the table, which reduces the number of duplicate calculations.

classSolution { Public:    DoubleMypow (DoubleXintN) {if(x = =0)return 0; if(n = =0)return 1; if(N <0)return 1.0/MYPOW (x,-N); Vector<Double>Power; Doubleres =1.0; Doublef =x; intloop =1;        Power.push_back (f);  while(Loop <<1) <=N) {f*=F;            Power.push_back (f); Loop<<=1; } N-=Loop; Res*=F; Bitset<sizeof(int) *8>POWB (n);  for(inti =0; I < +; ++i) {if(Powb[i] = =1) Res*=Power[i]; }        returnRes; }};

but this approach will appear Memory Limit exceeded. Because the input exponent is of type int, the actual vector<double> maximum length is 32, which is the constant complexity. The complexity of time is also at the constant level. I do not know what level of space complexity the topic limits.

Solution 3: Consider the use of binary search. If n is an even number, then POW (x,n) =pow (X,N/2) *pow (X,N/2), and if n is an odd number, multiply by x. So the recursion goes down until the n==0.

classSolution { Public:    DoubleMypow (DoubleXintN) {if(x = =0)return 0; if(n = =0)return 1; if(N <0)return 1.0/Power (x,-N); returnPower (x, N); }Private:    DoublePowerDoubleXintN) {if(n = =0)return 1; DoubleHalf = Power (x, N/2); Doubleres = half *half; returnN%2==0? Res:res *x; }};

[Leetcode]75. Pow (x,n) power operation

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.