Hello, I am trying a leetcode problem basically for the first time (tried a few weeks ago and didnt know vectors so didnt get very far). I have just went over vectors the past few days and now doing a problem which uses one. Im sure the way I am doing this problem is not the best way, but it is what I came up with but I can not seem to get it to return the indices needed.
The problem is
"Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice."
The code I have came up with so far is below. I think i just need to give back the two indices the it and it2 point to but I can not figure a way to do that. Is it even possible? I have marked with ****** where I believe i need to return the two indices. I was assuming something along the lines of return it, it2; but believe there is a conversion error with this.
you don't need a class in c++ to do a function, if you did not know.
there isnt anything wrong with it either.
n*n approach is usually a loser with these kinds of problems ... the point is to find some better way and they often throw you a couple million so that your answer will take too long.
dunno that its the 'best' but the 'obvious' improvement, with nothing clever, might go like this:
1) build your array as an array of structs of 2 integers (or at tuple if you know that or want to learn about it). one item will be the array index, so the [0] array location would have say (0, 1003) and the [1] location would have (1, 42) or whatever the numbers really are.
2) sort the array by the values (not the index values, you already have that!)
3) make an array of target - array value for each array data value.
4) search for the each value from #3 in the array made by sorting in #2 using binary search.
when you find it, the found value has its index on it, and the #3 array is in order so you know what index that piece had too, so you have both indices.
etc
that is NlgN instead of N*N, and the NlgN is from the sort. If you can counting sort instead or use any other integer exploits, it would be N instead of NlgN, which would really be quick.
Hi, I am still reading through the responses but just wanted to add that the site gave starter code, ie the class and vector return type. The full code that they supplied is below
1 2 3 4 5
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
};
They probably expect you to use indices and not iterators.
It's easy to return two ints in a vector<int>:
1 2 3 4 5 6 7 8 9 10
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for (int i = 0; i < int(nums.size()) - 1; ++i)
for (int j = i + 1; j < int(nums.size()); ++j)
if (nums[i] + nums[j] == target)
return {i, j};
return {-1, -1}; // not found
}
};
Thank you all for the replies. A lot of what Jonnin said I have not covered yet, such as tuples and binary search, but hopefully soon. And I see now that the solution dutch gave would have been a lot easier. I thought for once I had finally solved a problem almost to completion myself, but i guess not. My problem solving skills still are terrible xD.
EDIT:
Just while here I was wondering if someone could clarify something on vectors, and this problem, while doing it the way i was trying. The site says my output is [2] but im not understanding why this would be. I could understand if i was dereferencing it, but im not. Is it because im trying to return 2 iterator values?
Which, in my opinion, is fine for a beginner. DonnaPin's prompt does not mention iterators, so you might as well just use a classic i/j nested for loop and not deal with std::distance or iterator-index conversions.
(tried a few weeks ago and didnt know vectors so didnt get very far). I have just went over vectors the past few days and now doing a problem which uses one. Im sure the way I am doing this problem is not the best way, but it is what I came up with but I can not seem to get it to return the indices needed.
DonnaPin shows initiative, which is good in itself. However, at this point it is important to notice that vector can be iterated with index, just like plain array, and that that is more appropriate than the iterator-based approach for this problem.
Hi, thanks again for all the replies. Some of the stuff I have not covered yet such as a pair suggested by seeplus, so I will look into these things.
I think I really need to go over vectors a lot more in detail, an especially iterators. I think this problem has shown me that (even though for this solution I really didnt need them xD).