LeetCode summary, leetcode

Source: Internet
Author: User
Tags multiply strings

LeetCode summary, leetcode
Numerical Computation is very practical and common in the industry, so the frequency of interviews is very high. It is almost a required question. LeetCode has the following questions about numerical calculation:
Palindrome Number
Reverse Integer
Sqrt (x)
Pow (x, n)
Divide Two Integers
Max Points on a Line
In LeetCode, there are three types of questions about numerical operations. The following describes them one by one.

The first type is the simplest, that is, the direct operation on integers. Generally, it is a bitwise operation, such as reverse and comparison. In LeetCode, these questions include Palindrome Number and Reverse Integer. This type of questions is usually clearly thought-provoking. The important point to note is the consideration of the boundary condition. For numerical values, the main problem is the consideration of cross-border situations. In fact, the cross-border problem is a common problem of all numerical calculation questions. Most of the questions below will emphasize this.
In Palindrome Number, there is no cross-border problem because it is only used for judgment and does not need to modify numbers. The idea is relatively simple, that is, compare the highest bit and the lowest Bit every time, and return the result after the encounter or violation of the condition (that is, not equal. Due to the need to Reverse the number of the Reverse Integer, it is important to note that the number after the reversal may cross the border. Cross-border processing is generally two methods. One is to return the maximum (or minimum) number, and the other is to throw an exception. This can be discussed with the interviewer. Generally, for an interview, you only need to simply return the largest and smallest number or dummy number, but you must discuss with the interviewer the idea of handling and checking this corner case (that is, cross-border.

The second type of question is the question of arithmetic operations, such as multiplication and division, factorial, and square. In LeetCode, such questions include Sqrt (x), Pow (x, n), and Divide Two Integers. This kind of question sometimes seems complicated, but there are still several general solutions. The following mainly introduces three methods:
(1) binary classification. The bipartite method is a common and easy-to-understand method in numerical computation. The basic idea is to perform a half-cut operation, sometimes excluding half, and sometimes obtain reusable historical data. Sqrt (x) is a type that is excluded in half each time. If the request's square number is larger than the target number, it is split into half of the larger number; otherwise, it is split into half of the larger number, the principle is the same as binary search. Pow (x, n) is a type of reusable data, because the Npower of x is actually a multiplication of the n/2 power of two x, so we only need to recursively obtain the 1/2 power of the current x index at a time, and then multiply the two to get the final result. The bipartite method is obviously half the solution each time, so the time complexity is usually O (logn) magnitude.
(2) Newton's method. This method can be said to be mainly a mathematical method. If you do not know the principle, You can first look at the Newton method-Wikipedia. Sqrt (x) is very suitable for solving with the Newton method, because the items in its recursive formula are relatively simple. Of course, the theory of Pow (x, n) can also be solved by the Newton method. However, because the item in its recursive formula is the nth power of x, the cost of this calculation is also quite large, if we go to each step to obtain the Npower of x, it does not have much practical significance. Therefore, we generally do not need the Newton method for this question.
(3) displacement method. This method is mainly based on any integer that can be expressed as a linear combination of a group of bases based on the power of 2. It performs multiple-digit Iterative Solutions for an integer because the complexity is the number of digits, therefore, like the bipartite method, it is O (logn) magnitude. Pow (x, n) and Divide Two Integers are typical problems that can be solved in this way. For Pow (x, n), n can be divided into bits, and each left shift happens to be the square of the current number. Therefore, the result can be obtained after the number of iterations, A large part of the code is dealing with out-of-bounds issues, but the bit-by-bit iteration code is very short. Divide Two Integers also splits the result into bits. Each time the divisor is displaced and the corresponding divisor is subtracted to determine the result of each bit. This method may not be as intuitive as the binary method, but you need to digest it.

The third type of question is the question of analytic ry. Generally, the model for parsing geometric questions is complicated, and there are many implementation details, which are not common in interviews, in LeetCode, only Max Points on a Line belongs to this type of question. There is no general method for this question. It is mainly to clarify the mathematical and geometric models. For example, in Max Points on a Line, it is mainly to understand the judgment formula of the judgment point in a straight Line, and then implement it iteratively. There are still many implementation details and some boundary conditions need to be carefully considered.

This article mainly lists the questions about numerical calculation in LeetCode, introduces the main test points of such problems (such as cross-border judgment) and several common practical methods, in general, this type of problem is a problem that is more difficult to write right during the interview, because there are some boundary conditions and numerical implementation details. Because the frequency is very high, you still need to focus on this type of questions.
What is leetcode?

There are many programming and interview questions, which can be compiled and run online. It is difficult. If you can do it all by yourself, it is very helpful for large companies. I just did the questions there.

Runtime error occurs in multiply strings in leetcode.

Leetcode is positioned as an interview question, not an ACM question
That is to say, the question is not determined by AC or not whether your answer is correct or not.
What's more, give others a feeling during the interview.

This was originally a question about the string representing the large number and the uncle multiplication, but the po master used stringstream and vector
The answer in the interview won't leave a good impression on the interviewer even if the answer is correct.

I will help you debug the program, but I still think it is better to write a new one that is easy to understand and avoid using STL and stream.

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.