Java for Leetcode 018 4Sum

Source: Internet
Author: User

Given an array S of n integers, is there elements a, b, C, and D in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Problem Solving Ideas:

The first is definitely a sort, and if it's a brute-force enumeration, it's definitely timed out. Therefore, we can use the idea of divided treatment, storage of all 2 elements of the and, and then use the idea of 2SUM to solve, so the time complexity of O (n^2)

The Java implementation is as follows:

Java for Leetcode 018 4Sum

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.