Returns the subscript (base 1) of the elements in the array as input values

Source: Internet
Author: User

Leetcode Central Plains: The Sum of the sums

Given an array of integers, find the subscript where the number is equal to the given target value (the subscript for the first element of the array is 1), the first subscript must be smaller than the second subscript, assuming that the problem has only a unique solution.

Cases:

Input: numbers={2, 7, one, all, target=9

Output: Index1=1, index2=2

Idea 1: Two-layer loop, outer loop from 0 to penultimate element, Inner loop, starting from the next value of the current outer loop subscript to the end of the array, Complexity O (N2), timeout.

Idea 2: If the array is ordered, there is obviously a better way. The time complexity is O (NLOGN) by using the commonly used high efficiency sorting algorithm, merging sorting, heap sorting, etc. Because we need to record the subscripts in the original array, we cannot sort on the original array, we need to copy the original array and sort on the copy array. There are two things you need to do after you finish the sequence:

1) Find and two elements for the target value

2) Find out the subscripts of these two elements in the original array.

wherein, operation 1) sets two pointers, groups the head and tail of the array, calculates the sum of the values of the pointers, if the sum of the two is greater than the target value, then moves the trailing pointer forward, if the sum of the two is less than the target value, then moves the header pointer back so that the time complexity of finding the element is O

Operation 2) only need to traverse the original array once, you can obtain the corresponding subscript, the corresponding time complexity of O (n).

Combining the above solving process, the whole time complexity is O (NLOGN) +o (n) +o (n) =o (NLOGN).

The source code is as follows:

classSolution { Public: Vector<int> Twosum (vector<int> &numbers,inttarget) {Vector<int>temp (numbers); Vector<int>ret; Merge_sort (temp,0, Temp.size ()-1); //merge sorting of copy arrays inti =0, J=temp.size ()-1, K; intTemp_first,temp_second; //Record the temporary variable for the element value you are seeking
//Find elements that meet and target values in the sorted array while(i<j) {if((Temp[i]+temp[j]) <target) {i++; } Else if((Temp[i]+temp[j]) >target) {J--; } Else{Temp_first=Temp[i]; Temp_second=Temp[j]; Break; }} k=0;
//Traverse the original array to find the subscript of the corresponding element for(K;k!=numbers.size (); k++) { if(numbers[k]==temp_first| | numbers[k]==Temp_second) {Ret.push_back (k+1); } } returnret; } voidMerge_sort (vector<int> &ivec,int,vector<int>:: Size_type size); voidMerge (vector<int>&ivec,intPintQintr);};voidSolution::merge_sort (vector<int> &ivec,intP,vector<int>:: Size_type R) { intQ; if(p<r) {Q= (p+r)/2; Merge_sort (IVEC,P,Q); Merge_sort (Ivec,q+1, R); Merge (Ivec,p,q,r); }}voidSolution::merge (vector<int>&ivec,intPintQintR) { intI=0, j=0, k=p; Vector<int>::iterator iter =Ivec.begin (); Vector<int> Lvec (iter+p,iter+q+1); Vector<int> Rvec (iter+q+1, iter+r+1); while(I<lvec.size () &&j<rvec.size ()) { if(lvec[i]<=Rvec[j]) ivec[k++]=lvec[i++]; Elseivec[k++]=rvec[j++]; } if(i<lvec.size ()) { for(k;k<=r;k++,i++) Ivec[k]=Lvec[i]; } Else { for(k;k<=r;k++,j++) Ivec[k]=Rvec[j]; }}

Returns the subscript (base 1) of the elements in the array as input values

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.