/
1.cpp
48 lines (45 loc) · 1.16 KB
/
1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* @author 707<707472783@qq.com>
* Question:Given an array of integers, return indices of the two numbers such that they add up to a specific target.
*/
#include <iostream>
#include <vector>
using std::vector;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target);
int findPos(vector<int>& nums, int num, int exist);
};
vector<int> Solution::twoSum(vector<int>& nums, int target) {
vector<int> result;
for (size_t i = 0; i < nums.size(); i++) {
int anotherPos = findPos(nums, target - nums[i], i);
if (anotherPos >= 0) {
result.push_back(i);
result.push_back(anotherPos);
break;
}
}
return result;
}
int Solution::findPos(vector<int>& nums, int num, int exist) {
for (size_t i = 0; i < nums.size(); i++) {
if (nums[i] == num) {
if (i == exist) {
continue;
}
return i;
}
}
return -1;
}
int main() {
Solution s;
vector<int> v = {-1,-2,-3,-4,-5};
vector<int> r = s.twoSum(v, -8);
for (auto i : r) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}