LeetCode summary, leetcode

Source: Internet
Author: User

LeetCode summary, leetcode
Merging is a common operation in one-dimensional data structures, usually sorting, suboperations in distributed algorithms. This summary mainly introduces several questions about merging in LeetCode:
Merge Two Sorted Lists
Merge Sorted Array
Sort List
Merge k Sorted Lists

Let's take a look at the merging of two ordered one-dimensional data. Here we mainly want to introduce the merging operation of the linked list. However, because the merging of one-dimensional arrays is also relatively simple and has a comparison with the linked list, by the way, let's list them here. Merge Two Sorted Lists requires that Two ordered linked Lists be merged. Generally, the merging idea is to take one as the main reference, and then compare items one by one. If a small element is in the reference linked list, move on, otherwise, insert the node into the reference linked list and move forward to another linked list. Finally, if the other linked list has not yet reached the head, it can be directly taken over. The ideas and implementations are relatively simple, it is best to try again. The same idea is true for Merge Sorted Array, except that the data structure is replaced with a one-dimensional Array. Therefore, the insert operation is more complicated than the linked list. This is equivalent to starting from the end, and then entering the numbers one by one based on their positions, the insert operation is not obvious here. It can be seen that although the idea is similar, but the data structure is different, because the operation is different, the implementation details are still quite different, you have to master it.
With the above two linked lists (or arrays) merged, we can Sort them, that is, the Sort List. For a linked list, use Merge Two Sorted Lists as the sub-operation for merging, and then use recursive Merge to split and Merge. Here we will not talk about the specific details of sorting, there will be a special sorting summary article.

Finally, the most important and practical question is Merge k Sorted Lists. Why is it practical? It is because there are many distributed systems and MapReduce or Hadoop technologies are also emphasized. This topic is a common operation of distributed computing. For example, ordered data from different clients should be merged on the central server. There are two ways to solve this problem:
The first method is to use the above Merge Two Sorted Lists to Merge the k linked Lists first, and then combine them on the previous layer until a linked list is merged. According to the analysis in Merge k Sorted Lists, the time complexity is O (nklogk), and the space complexity is O (logk ).
The above method is to use the sub-governance method and then merge. Next, This method uses the heap data structure. The idea is to maintain a heap with a size of k, put the minimum element at the top of the heap to the result, and then read the next element of the linked list corresponding to the element into the heap. Because each linked list is ordered, and each time it is the smallest of the current k elements, when all linked lists are read, all elements are placed in the result linked list from small to large. This algorithm reads each element once, that is, k * n times. Then, each read element inserts the new element into the heap and requires the complexity of O (logk). Therefore, the complexity is O (nklogk ), this is the same as the first method.
The data structures and types of the two methods are very representative. I personally think they are very interesting.

Merging is a typical operation in a linked list. It can also continuously ask some coherent questions, which is highly scalable and can be used to evaluate the operation of the basic data structure, we can also look at the understanding of algorithms and more data structures, which is a good interview topic.
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.