首页 > 代码库 > leetcode 2SUM
leetcode 2SUM
struct vp{
int value;
int place;
};
bool comp(const struct vp a, const struct vp b){
return a.value<b.value;
}
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<struct vp> v ;
for(int i = 0; i < numbers.size(); ++i){
struct vp tmp;
tmp.value = numbers[i];
tmp.place = i;
v.push_back(tmp);
}
sort(v.begin(), v.end(),comp);
for(int i = 0; i < v.size(); i++){
for(int j = i+1; j < v.size(); j++){
if(v[i].value + v[j].value > target){
break;
}
if(v[i].value + v[j].value < target){
continue;
}
if(v[i].value + v[j].value == target){
vector<int> t ;
t.push_back(v[i].place+1);
t.push_back(v[j].place+1);
sort(t.begin(),t.end());
return t;
}
}
}
return numbers;
}
};
int value;
int place;
};
bool comp(const struct vp a, const struct vp b){
return a.value<b.value;
}
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<struct vp> v ;
for(int i = 0; i < numbers.size(); ++i){
struct vp tmp;
tmp.value = numbers[i];
tmp.place = i;
v.push_back(tmp);
}
sort(v.begin(), v.end(),comp);
for(int i = 0; i < v.size(); i++){
for(int j = i+1; j < v.size(); j++){
if(v[i].value + v[j].value > target){
break;
}
if(v[i].value + v[j].value < target){
continue;
}
if(v[i].value + v[j].value == target){
vector<int> t ;
t.push_back(v[i].place+1);
t.push_back(v[j].place+1);
sort(t.begin(),t.end());
return t;
}
}
}
return numbers;
}
};
leetcode 2SUM
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。