26 lines
722 B
C++
26 lines
722 B
C++
#include <vector>
|
|
#include <map>
|
|
|
|
using namespace std;
|
|
|
|
namespace TwoSum{
|
|
class Solution {
|
|
public:
|
|
vector<int> twoSum(vector<int>& nums, int target) {
|
|
vector<int> res;
|
|
map<int, int> hashMap;
|
|
for (int i = 0; i < nums.size(); i++)
|
|
{//Loop through Dataset
|
|
if (hashMap.find(target - nums[i]) != hashMap.end())
|
|
{//See if the remaining value is in the map
|
|
res.push_back(i);
|
|
res.push_back(hashMap[target - nums[i]]);
|
|
return res;
|
|
}
|
|
else
|
|
hashMap.emplace(nums[i], i);
|
|
}
|
|
return res;
|
|
}
|
|
};
|
|
} |