[Leetcode]two sum (traversal hash)

Source: Internet
Author: User
Tags hash
The Sum of

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

Enter a number, given a target and, to find out that a number of two numbers add equal to the target and output the position of the two digits (small left is large).

Parse: Iterate through this array, reading each number as it is, and see if (and-numbers) exist in the array, output break

Class Solution {
 2 public:
 3     vector<int> twosum (vector<int> &numbers, int target) {
 4< C4/>unordered_map<int, Int> MP;
 5         vector<int> ans;
 6 for         (int i = 0; i < numbers.size (); i + +)
 7         {
 8             if (Mp.count (target-numbers[i))
 9             {                 ans.push_back (Mp[target-numbers[i]] + 1);                 Ans.push_back (i + 1);
Break                 ;
+             if (!mp.count (Numbers[i])) mp[numbers[i]] = i;
+         return ans;
18};

Iterate through the array only once, with an O (n) complexity.

Unordered Map

A

Hash map is an associative container that stores elements through key values and mapped values. Allows quick retrieval of individual elements based on key values.
in Unordered_map, the key value is typically used to uniquely identify an element, and the corresponding value is the content of an object associated with the key. The type of the key mapping value may vary. The
elements inside the unordered_map are not sorted in any particular order by key or mapped elements, and their storage location depends on the hash value allowing for quick access to a single element (with a constant average time complexity) directly through its key value. The
Unordered_map container accesses their individual elements more quickly than the map container through key values, although unordered_map is generally less efficient than a subset of maps through its elements. The
Hash map allows you to use the operator operator (operator []) to directly access an element with its key value as a parameter.

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.