Leetcode (1) | | The Sum of

Source: Internet
Author: User

Leetcode (1) | | The Sum preface

Always want to brush under the leetcode of the topic, and finally in this year's first night of work started, just for my algorithm to learn the way to open a head. At present, there are 179 questions in Leetcode, for two months to finish the brush.

Twosum Problem Description

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

explanation of Problem solving
    • Leetcode to a variety of languages, because now engaged in Java-related work and Java debugging convenience, so the follow-up will be Java-based. Leetcode provides the following Java functions.
1  Public class Solution {2      Public int [] twosum (intint  target) {3         4    }5  }
    • The meaning of the title is roughly: given an array of integers numbers, and the integer target, which asks for two numbers A and B in the array to add and target, and a<b, the function returns the index number of a and B in the array (starting from 1). Requires as little computational complexity as possible.
    • This is a simple question at first glance, but it is a little difficult to take into account the computational complexity and the many situations (such as the addition of a negative number).
    • Solving this problem is not difficult, two for loop, the computational complexity of ο (N2) method can be obtained, but Leetcode run code is time-limited. So we need to consider the less complex, the ο (n) hash table. The core idea of problem solving is that if a+b=target, then A and b are hashed together so that the results can be calculated in a single loop.
    • In the process of solving the problem I made two mistakes, the first time is not to consider the negative situation, the second is not considered 3+3=6 this situation, this also educates me to do the algorithm when not only to consider the efficiency, but also to consider comprehensive.
Problem solving Results

I used Java's own hashmap to implement the hash.

1 ImportJava.util.HashMap;2 ImportJava.util.Map;3  Public classSolution {4      Public int[] Twosum (int[] numbers,inttarget) {5         intLen =numbers.length;6map<integer,integer> map =NewHashmap<integer,integer>();7         int[] result =New int[2];8          for(inti = 0; i < Len; i + +){9             TenInteger number =NewInteger (Numbers[i]); OneInteger index =NewInteger (i+1); A              -             if(!map.containskey (Target-Number )) { - map.put (number, index); the}Else if(Map.containskey (target-Number )) { -result[0]= Map.get (target-Number ); -Result[1]=i+1; -                  Break; +             } -         } +         returnresult;  A     } at}

Operation Result:

Looking at the results of the operation, it is not difficult to find that Java running time is significantly greater than C and C + +, so the same method implemented in C + +:

1 classSolution {2  Public:3vector<int> Twosum (vector<int> &numbers,inttarget) {4         inti, sum;5vector<int>results;6map<int,int>Hmap;7          for(i=0; i<numbers.size (); i++){8             intnum =Numbers[i];9             if(!hmap.count (target-num)) {TenHmap.insert (pair<int,int> (Numbers[i], i+1)); One}Else if(Hmap.count (target-num)) { AResults.push_back (hmap[target-Numbers[i]]); -Results.push_back (i+1); -                  Break; the             } -         } -         returnresults; -     } +};

This shows that the same approach, implemented in C + + is nearly 10 times times faster than Java.

Leetcode (1) | | The Sum of

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.