Two Sum

来源:互联网 发布:爷爷9岁被鬼子杀了知乎 编辑:程序博客网 时间:2024/05/18 02:42

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 twic
这里写图片描述

vector<int> twoSum(vector<int> &numbers, int target){    unordered_map<int, int> hash;    vector<int> result;    for (int i = 0; i < numbers.size(); i++)    {        int numberToFind = target - numbers[i];        if (hash.find(numberToFind) != hash.end())        {            result.push_back(hash[numberToFind]);            result.push_back(i);        }        hash[numbers[i]] = i;    }    return result;}
0 0