Reputation: 3
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class twoSumClass{
public:
vector<int> twoSum(vector<int>& nums, int target){
vector<int> result;
unordered_map<int,int> hm;
for(int index = 0; index < nums.size(); index++){
int findVal = target - nums[index];
if(hm.count(findVal) > 0 && hm[findVal] != index){
result.push_back(index);
result.push_back(hm[findVal]);
return result;
}
else{
hm.insert(make_pair(nums[index], index));
}
}
return result;
}
};
void Print(vector<int>& v){
for (int i = 0; i < v.size(); i++){
cout << v[i] <<endl;
}
};
int main(){
vector<int> items = {1,2,3,4,5,6,7};
int trgt = 2;
twoSumClass myTest;
std::vector<int> res = myTest.twoSum(items, trgt);
Print(res);
return 0;
}
this is my simple code, I am trying run this code in linux terminal as: g++ test1.cpp -o test1, and then run: ./test1 however, terminal does not print anything. how do i change code? BTW, the purpose of code is similar with leetcode question #1 add two sum.
Upvotes: 0
Views: 499