LeetCode summary, leetcode

Source: Internet
Author: User

LeetCode summary, leetcode
This article mainly introduces several questions about kSum in LeetCode. The main requirement is to find the number of k in the array and whether it can reach the target value. LeetCode:
Two Sum
3Sum
3Sum Closest
4Sum
First of all, starting with Two Sum, this question provides the basic idea of the next k> 2 questions and is also the most frequently tested questions (Amazon's interview is just a special liking for this question ). There are two main ideas: one is to use a hash table to record the appearance of elements, and then check whether the new elements can be matched with existing elements. If the access to the hash table is a constant operation, the complexity of this algorithm is O (n ). Another idea is to sort the array first, and then use the folder-Forced method to find the pair that meets the conditions. The complexity of the algorithm is O (nlogn ). The Two methods are better than the O (n ^ 2) of brute force. For more information, see the analysis of Two Sum. This model is simple, but it can be used to evaluate basic algorithms such as hash tables and data structures and sorting. Therefore, it is a frequent visitor during the interview.
Next we will use 3Sum and 3Sum Closest. 3Sum uses the second method of Two Sum as a sub-operation, and then loops through each element to find the pair with the remaining elements meeting the condition. 3Sum Closest is very similar to 3Sum, but it only needs to maintain a minimum diff and save the value Closest to the target. The complexity of the algorithm is O (n ^ 2). The first method to use Two Sum is not suitable because it is not convenient to use a hash table when elements are duplicated.
The last step is 4Sum. The optimal solution for this question is to use the kSum solution to solve the problem layer by layer and then combine it with Two Sum. The basic idea is as follows, the implementation details are still complicated, even if the 4Sum is complicated, so the difficulty and implementation details during the interview are here, for general questions such as kSum, you only need to know your ideas to meet the interview requirements.
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.

What is blog?

The full name of a blog should be Web log. The Chinese name is "network log", which is abbreviated as Blog, and the Blog (Blogger) is the person who writes the blog. In terms of understanding, a blog is a publishing method that expresses personal thoughts, network links, and content in chronological order ". Simply put, blogs are a type of people who are used to writing diaries online.

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.