Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
最朴素的方法
#include <iostream>
#include<vector>
using namespace std;
class Solution {
public:
vector<int> two_sum (const std::vector<int> &a, int target ){
for(int i=0;i<a.size();i++) {
for(int j=0;j<a.size();j++) {
if(a[i]+a[j] == target) {
vector<int> b;
b.push_back(i);
b.push_back(j);
return (b);
}
}
}
}
};
int main(void) {
int myc[4] = {2,7,11,15};
vector<int> c(myc,myc+4);
Solution leetcode;
vector<int> re = leetcode.two_sum(c,17);
for(int i=0; i<re.size();i++)
cout<< re[i];
cout<<"\n";
}