Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <bits/stdc++.h>
- #include <string>
- using namespace std;
- vector<int> twoSum(vector<int> nums, int target)
- {
- vector<int> result;
- for (unsigned long int i1 = 0; i1 < nums.size()-1; ++i1)
- {
- for (unsigned long int i2 = i1+1; i2 < nums.size(); ++i2 )
- {
- if (nums[i1] + nums[i2] == target)
- {
- result.push_back(i1);
- result.push_back(i2);
- }
- if (result.empty())
- result = {0};
- return result;
- }
- }
- }
- vector<int> splitBySpace(string str)
- {
- vector<string> sresult;
- vector<int> result;
- string word = "";
- for (auto x : str)
- {
- if (x == ' ')
- {
- sresult.push_back(word);
- word = "";
- }
- else
- {
- word = word + x;
- }
- }
- for (string a : sresult)
- {
- result.push_back(stoi(a));
- }
- return result;
- }
- int main()
- {
- vector<int> inputnums;
- int target;
- string input;
- cout << "Enter your integer numbers array: " << endl;
- cin >> input;
- cout << "Enter desired target: " << endl;
- cin >> target;
- vector<int> tmp = splitBySpace(input);
- vector<int> finalv = twoSum(tmp, target);
- for (int i : finalv)
- cout << i << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement