打算找工作了。把leetCode的题集刷一刷。
第一个题很简单,但是做出来比较麻烦,出错的地方比较多。
思路:2分查找。
不会的知识点:C++ 的 模板函数。回来打算把C++Primer 刷一遍。都搞清楚。
我把自己的写好的代码贴上来。(竟然28ms是很慢的。。。。。)
multimap<int,int>::iterator head,last,it;
vector<int>::iterator jt;
multimap<int,int> q;
vector<int>ans;
int ind=0;
for(jt = nums.begin();jt!=nums.end();++jt)
{
q.insert(make_pair(*jt,ind));
ind++;
}
head = q.begin();
last = q.end();
int res,indx,indy;
it = head;
res = target - it->first;
it = head;
indx = head->second;
while( head != last)
{
++it;
// cout<<res<<endl;
if(res<it->first)
{
last = it;
++head;
indx= head->second;
it = head;
res = target - it->first;
}
else if(res == it->first)
{ indy = it->second;
ans.push_back(indx);
ans.push_back(indy);
sort(ans.begin(),ans.end(),less<int>());
// q.clear();
return ans;
}
if(it == last)
{
head++;
it = head;
indx = head->second;
res = target - it->first;
}
}