This is one of those problems where the clean idea matters more than the amount of code. For 50. Pow(x, n), the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are myPow.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 4 ms, faster than 23.29% of C++ online submissions for Pow(x, n).
02//Memory Usage: 6.1 MB, less than 20.88% of C++ online submissions for Pow(x, n).
03class Solution {
04public:
05 double myPow(double x, int n) {
06 return pow(x, n);
07 }
08};
09
10//divide and conquer, recursion
11//https://leetcode.com/problems/powx-n/discuss/19546/Short-and-easy-to-understand-solution
12//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Pow(x, n).
13//Memory Usage: 5.9 MB, less than 95.42% of C++ online submissions for Pow(x, n).
14class Solution {
15public:
16 double myPow(double x, int n) {
17 if(n == 0){
18 return 1;
19 }
20
21 if(n == INT_MIN){
22 /*
23 if n is INT_MIN(-2147483648),
24 -n will exceed INT_MAX(2147483647),
25 leading overflow
26
27 we can avoid overflow by adding INT_MIN by 1,
28 because -2147483648/2 is the same as (-2147483648+1)/2
29 */
30 ++n;
31 n *= -1;
32 x = 1/x;
33 return myPow(x*x, n/2);
34 }
35
36 if(n < 0){
37 if(n == INT_MIN) ++n;
38 n *= -1;
39 x = 1/x;
40 }
41
42 return myPow(x*x, n/2) * (n%2 ? x : 1);
43 }
44};
45
46//iterative
47//https://leetcode.com/problems/powx-n/discuss/19544/5-different-choices-when-talk-with-interviewers
48//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Pow(x, n).
49//Memory Usage: 6 MB, less than 60.70% of C++ online submissions for Pow(x, n).
50class Solution {
51public:
52 double myPow(double x, int n) {
53 if(n == 0 || x == 1){
54 return 1;
55 }
56
57 double ans = 1.0;
58
59 if(n == INT_MIN){
60 //take -1 power away
61 ans /= x;
62 ++n;
63 n *= -1;
64 x = 1.0/x;
65 }else if(n < 0){
66 n *= -1;
67 x = 1.0/x;
68 }
69
70 while(n){
71 if(n & 1){
72 ans *= x;
73 }
74
75 x *= x;
76 n >>= 1;
77 }
78
79 return ans;
80 }
81};
Cost