Catalog
1. 1. 1. Two Sum
LeetCode Solution: 1. Two Sum

## 1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

map容器是一个存放关键字和值对的容器，关键字可以是任意类型，直接访问关键字下标可以得到对应的值，用到了C++STL里面的unordered_map

1. 如果wanted_numhash里的话，就把wanted_num的索引和当前的i放入result中，作为最后结果
2. 如果没有在hash表里面，当前的元素放入hash表里，并设置下标为i

Author: Jsthcit
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

Comment