LeetCode (1) Two Sum

Source: Internet
Author: User

LeetCode (1) Two Sum
Question:

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

Analysis: The question is marked as Medium, and the difficulty is moderate. After reading the question, the first reaction is to traverse the array twice, and some judgment can be solved. Then I wrote the code, and I was confident to submit it:
class Solution{public:    vector
 
   twoSum(vector
  
    &numbers , int target)    {        vector
   
     index;        for(int i=0 ; i!=numbers.size(); i++)        {            for(int j=numbers.size()-1 ; j>i; j--)                if((numbers[i]+numbers[j]) == target)                {                    index.push_back(i+1);                    index.push_back(j+1);                    break;                }        }//for        return index;    }//twoSum};
   
  
 
That's right, I think, isn't it true that LeetCode is of medium difficulty so that it can be AC... So I got a response like this: Status: Time Limit Exceeded re-analysis: re-examine the question, the above O (n ^ 2) algorithm must not meet the requirements, what methods should be used to avoid overlap loops? If there is no answer to the problem, you can only seek help from Baidu. After scanning a netizen's analysis, I suddenly realized what we learned about hash, because it has inherent complexity advantages. Below is the AC code:
Class Solution {public: vector
 
  
TwoSum (vector
  
   
& Numbers, int target) {vector
   
    
Index; map
    
     
HashMap; for (unsigned int I = 0; I
     
      
In this way, the algorithm complexity is reduced to O (n), and the first LeetCode question is successfully AC. It seems that the water is still home, and you need to continue to work! Test the main function: In order to facilitate program testing, the main function code is also provided for reference:
      
int main(){    Solution s;    int arr[3] = {3,2,4};    int target = 6;    vector
       
         numbers(arr , arr+3);    vector
        
          index;    index = s.twoSum(numbers , target);    cout<<"index1="<
        
       

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.