PS: Sword finger offer is not only an interview guide that many students will refer to when looking for a job, but also an algorithm Guide (the main reason why it is so popular is that it provides a step-by-step optimization solution, which I think is very friendly). Now many Internet algorithm interview questions can be found here. For the convenience of reference and review in the future, the examples in the book are implemented in Java (Second Edition). You are welcome to communicate and progress together.

GitHub: https://github.com/Uplpw/SwordOffer.

Full title link: https://blog.csdn.net/qq_41866626/article/details/120415258

# 1 Title Description

Realize pow(x, n), that is, calculate the N-power function of X (i.e., x^n). Library functions must not be used and large numbers do not need to be considered.

leetcode link: Integer power of value (the following code has been tested and submitted)

# 2 test cases

Generally, functional use cases, special (edge) use cases, counterexamples and invalid test cases are considered. We can even find some rules to solve problems from test cases, and it can also make our program more complete and robust.

(1) Function use case: normal x and n values, such as 3 and 4.

(2) Edge use case: n = 0, 1, negative.

(3) Invalid use case: base is 0.

# 3 ideas

analysis:

Since the problem of large numbers is not considered, the workload is less, but we should pay attention to the details and consider the special cases according to the test cases thought out in advance.

Note: to judge whether the floating-point number is 0 is not to directly compare it with 0, but to judge whether the difference between the two is within this precision.

The following is the specific process of two solutions:

Solution 1: cyclic iteration:

This method is the simplest, direct and easiest to think of. However, pay attention to several situations. The data is meaningless. Pay attention to the processing method (prompt the error with the return value of the function; prompt the error with a global variable; throw an exception;).

- When the base number is 0, (1) the index is negative, then the denominator is 0, meaningless, and an exception can be thrown; (2) Other values return 0.
- When the exponent is positive, the iterative cycle can be solved directly;
- When the index is negative, the general process is to convert the opposite number into a positive index, and then find the reciprocal. There is a special case that needs attention, that is, the index n = Integer.MIN_VALUE. At this time, its opposite number is still itself. Therefore, there will be problems in solving this problem (especially the following recursive solution directly leads to stack overflow). To solve this problem, we can first calculate a base number, and then n=-n-1 to avoid this special situation (see the code for details).

Solution 2: recursion

- seek x n x^n xn, we can turn it into x n / 2 ∗ x n / 2 x^{n /2}*x^{n /2} xn/2 * xn/2 or x ∗ x n / 2 ∗ x n / 2 x*x^{n /2}*x^{n /2} x * xn/2 * xn/2 (n-point parity), so that recursive solution can be used to reduce the time complexity by exchanging space for time.
- Also pay attention to several special cases in solution 1, especially when the exponent n = Integer.MIN_VALUE. I won't repeat it here.

# 4 code

Algorithm implementation:

public class Power { // Loop calculation should pay attention to special cases, and exceptions can be thrown public static double power(double x, int n) throws Exception { if (equal(x, 0)) { if (n == 0) { return 1; } else if (n < 0) { throw new Exception("The denominator is 0"); } return 0; } if (n < 0) { double pow = x; int count = -n - 1; for (int i = 1; i < count; i++) { pow = pow * x; } return 1 / pow; } else { double pow = 1; for (int i = 0; i < n; i++) { pow = pow * x; } return pow; } } // Recursive calculation, you can throw exceptions public static double powerRecursionly(double x, int n) throws Exception { if (equal(x, 0)) { if (n == 0) { return 1; } else if (n < 0) { throw new Exception("The denominator is 0"); } return 0; } if (n < 0) { return 1 / x * powerCoreRecursionly(1 / x, -n - 1); } else { return powerCoreRecursionly(x, n); } } public static double powerCoreRecursionly(double x, int n) { if (n == 0) { return 1; } else { if ((n & 1) == 0) { double temp = powerCoreRecursionly(x, n >> 1); return temp * temp; } else { double temp = powerCoreRecursionly(x, n >> 1); return x * temp * temp; } } } // To judge whether double types are equal, it is mainly to judge whether the difference between the two is within a small range public static boolean equal(double x, double y) { return -0.000001 < (x - y) && (x - y) < 0.000001; } public static void main(String[] args) throws Exception { System.out.println("2^3=" + powerRecursionly(2, 3)); System.out.println("2^-3=" + powerRecursionly(2, -3)); System.out.println("0^3=" + powerRecursionly(0, 3)); System.out.println("0^-3=" + powerRecursionly(0, -3)); } }

reference resources

When solving the examples in this book, we refer to the solutions of some big guys, such as the official, K God and other blogs on leetcode. After the detailed explanation of each example, we will give the reference ideas or code links. Students can click in and have a look!

Reference to this example:

https://www.jianshu.com/p/83b5663a519b

If there are any deficiencies or mistakes in this article, you are welcome to criticize and correct. Finally, I hope to communicate with you and make progress and get your favorite offer!!!