LeetCode_Two Sum, leetcode_twosum
I. QuestionsTwo Sum Total Accepted:69820Total Submissions:386081My Submissions
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum shocould return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input wowould have exactly one solution.
Input:Numbers = {2, 7, 11, 15}, target = 9
Output:Index1 = 1, index2 = 2
Show TagsHave you met this question in a real interview? Yes No
Discuss
Ii. problem-solving skillsThe simplest way to solve this problem is to use the brute force algorithm. Although the space complexity is O (1), the time complexity is O (n ^ 2 ). The brute-force algorithm I used at the beginning directly prompts that the running time is too long after the result is submitted. Therefore, the answer to this question is not a brute-force algorithm. If array A with n elements is sorted, Set Head = 0, End = n-1. For A [Head] + A [End] and target, there are three situations:
1. (A [Head] + A [End]) = target, then A [Head] And A [End] are two elements that can meet the conditions and the judgment ends. 2. (A [Head] + A [End])> target and A [End] cannot be elements that meet the conditions. Therefore, End = End-1 and continue to be judged. (A [Head] + A [End]) <target, A [Head] cannot be an element that meets the conditions. Therefore, Head = Head + 1 is used for further judgment;
According to the preceding method, the boundary condition is: When Head> = End, the sum of the two elements is equal to the given value. The time complexity of this method is O (n), and the space complexity is O (1 ).
The premise of the above method is that the array has been sorted, and the time complexity for sorting unordered arrays is O (nlogn). Therefore, first sort the input array, then we use the above algorithm to find two elements that meet the conditions. The time complexity is O (nlogn) + O (n) = O (nlogn ), in this way, the time complexity relative to the O (n ^ 2) of the brute force algorithm is greatly reduced.
Although the above algorithm can find two elements that meet the conditions, this question does require that the coordinates of the elements be calculated. Therefore, you must first copy the original array, then, perform the above algorithm in the copied array, find the element, and then find the coordinate value in the original array. Because the array may have the same elements and the two elements meeting the conditions are the same, after finding the coordinates of the first element, you need to change the value of the first element, for convenience, you can change the value of the first element to the second element minus 1, and then look for the coordinates of the second element.
Iii. Implementation Code
class Solution{public: vector<int> twoSum(vector<int> &numbers, int target) { // copy the numbers vector<int> TmpNumbers(numbers); // sort the tmp number vector sort(TmpNumbers.begin(), TmpNumbers.end()); int Size = TmpNumbers.size(); int HeadIndex = 0; int EndIndex = Size - 1; int Start = 0; int End = 0; int SumOfTwo = 0; while(HeadIndex < EndIndex) { Start = TmpNumbers[HeadIndex]; End = TmpNumbers[EndIndex]; SumOfTwo = Start + End; if (SumOfTwo == target) { break; } if (SumOfTwo > target) { EndIndex--; } if (SumOfTwo < target) { HeadIndex++; } } // find the element vector<int>::iterator Ite_1 = find(numbers.begin(), numbers.end(), Start); (*Ite_1) = End - 1; vector<int>::iterator Ite_2 = find(numbers.begin(), numbers.end(), End); // get the index of the element int Index_1 = Ite_1 - numbers.begin() + 1; int Index_2 = Ite_2 - numbers.begin() + 1; if (Index_1 > Index_2) { int Tmp = Index_1; Index_1 = Index_2; Index_2 = Tmp; } vector<int> Result; Result.push_back(Index_1); Result.push_back(Index_2); return Result; }};
Iv. ExperienceThis is also a question that uses the characteristics of the sorting array, but the original array is not sorted. After sorting the original array, you can use the characteristics of the sorting array for calculation. This is also a simple question.
All rights reserved. You are welcome to reprint it. Please indicate the source for reprinting. Thank you.