Nine-chapter algorithm surface Test 3 sum

Source: Internet
Author: User

Nine Chapters count judges Net-original website

http://www.jiuzhang.com/problem/68/


Topics

Give a bunch of arrays and a target value, and in this heap of arrays find three numbers that make their sum equal to the target value.


Online test

http://www.lintcode.com/en/problem/3-sum/


Answer

If the first element in the array of the original problem we use AI to express, the target value we use V to represent.

The method of this problem is very similar to the second solution of 2 sum. First of all, let's put the array in order first, then we still need three pointers i,j,k, we assume i<j<k, because we ordered, then A[i]<a[j]<a[k], actually test instructions can be converted to a[i]+a[j]+a[k]=v. If we enumerate I, then test instructions can be converted to an element greater than a[i] to find a[j]+a[k] = V-a[i], then this problem is 2sum, so we can use 2sum two pointers to one end of the method to solve the problem.

Nine-chapter algorithm surface Test 3 sum

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.