[leetcode]1. Two Sum
https://leetcode.com/problems/two-sum/
class Solution {
public:
typedef struct{
int index;
int data;
}E;
static bool cmp(E a, E b){
return a.data < b.data;
}
vector<int> twoSum(vector<int>& nums, int target) {
vector<E> container;
for(int i = 0; i < nums.size(); i++){
E e;
e.data = nums[i];
e.index = i;
container.push_back(e);
}
sort(container.begin(), container.end(), cmp);
int begin = 0, end = nums.size() - 1;
while(begin < end){
if(container[begin].data + container[end].data == target){
vector<int> r;
r.push_back(container[begin].index);
r.push_back(container[end].index);
return r;
}
else if(container[begin].data + container[end].data < target){
begin++;
}
else
end--;
}
}
};