# [Leetcode] 1. Two sums in the sum array and two numbers for a specific value

Source: Internet
Author: User

Given an array of integers, find the numbers such that they add up to a specific target number.

The function twosum should return indices of the numbers such that they add up to the target, where index1 must is Les S than Index2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.

You may assume this each input would has exactly one solution.

Input:numbers={2, 7, one, A, target=9
Output:index1=1, index2=2

Solution One: Brute force search method, Time complexity O (n^2)

`classSolution { Public: Vector<int> Twosum (vector<int> &numbers,inttarget) {Vector<int>Res;  for(inti =0; I < numbers.size (); ++i) { for(intj = i +1; J < Numbers.size (); ++j) {if(Numbers[j] + numbers[i] = =target) {Res.push_back (i+1); Res.push_back (J+1); }            }        }        returnRes; }};`

Solution Two: Consider sorting the array arr first, using two pointers left and right pointing to some two values of the ordinal group, initializing the Left=0,right=len-1, where Len is the array length. When Arr[left]+arr[right]=sum, find the answer to return, when Arr[left]+arr[right]>sum, when, right--; when Arr[left]+arr[right]<sum, left++. Loop the above process, and return if found, or until the left=right description has no solution. Finally, find the two-digit position in the original array that meets the requirements. Time complexity O (NLOGN).

`classSolution { Public: Vector<int> Twosum (vector<int>& Nums,inttarget) {Vector<int>VI (Nums.begin (), Nums.end ());                Sort (Vi.begin (), Vi.end ()); intIndex1 =0; intIndex2 = Nums.size ()-1;  while(Index1 <index2) {            if(Vi[index1] + vi[index2] = =target) Break; Else if(Vi[index1] + Vi[index2] <target) index1++; ElseIndex2--; } Vector<int> Res (2,0); inti =0;  for(; i < nums.size (); i++)        {            if(Nums[i] = =Vi[index1]) {res[0] = i +1;  Break; }        }        if(Vi[index1] = =Vi[index2]) I++; ElseI=0;  for(; i < nums.size (); i++)        {            if(Nums[i] = =Vi[index2]) {res[1] = i +1;  Break;                }} sort (Res.begin (), Res.end ()); returnRes; }};`

Solution Three: Consider using the map of STL to find. The array is traversed first, the key is the element value, and the value is the subscript of the element in the array. Then iterate through the array from the beginning, looking for target=sum-arr[i] elements in the map, and return if they exist, or until the i=len-1 description has no solution. Finally find the location of target in arr. Time complexity O (n).

`classSolution { Public: Vector<int> Twosum (vector<int> &numbers,inttarget) {Vector<int>Res; Map<int,int>Nummap;  for(inti =0; I < numbers.size (); ++i) {nummap[numbers[i]]=i; }         for(inti =0; I < numbers.size (); ++i) {intTMP = target-Numbers[i]; if(Nummap.find (tmp)! = Nummap.end () && nummap[tmp]! =i) {res.push_back (i+1); Res.push_back (Nummap[tmp]+1);  Break; }        }        returnRes; }};`

[Leetcode] 1. Two sums in the sum array and two numbers for a specific value

Related Keywords:

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.