题目
思路
代码实现
#include<iostream>
#include<unordered_map>using namespace std;class Solution
{
public:vector<int> TwoNumber(const vector<int>& nums, int target){vector<int> number_vector;unordered_map<int, int> hash_table;for (int i = 0; i < nums.size() ; i++){auto it = hash_table.find(target - nums[i]);if (it != hash_table.end()){number_vector.push_back(it->second);number_vector.push_back(i);return number_vector;}else{hash_table[nums[i]] = i;}}}
};int main()
{Solution test;vector<int> number_vector = {2,6,11,12,7,8};vector<int> out = test.TwoNumber(number_vector, 9);for (auto var : out){std::cout << "out:" << var << " " << std::endl;}return 0;
}
测试结果