-2

So I decided to give the easiest problem on Leetcode a try and since I dont data structures well enough to use maps or hashtables I knew I was gonna have to go for brute force. I am getting this error when I try to compile and it is for my return statements.

Line 9: Char 29: error: expected expression

                   return nums[] {i,i+1}

                               ^

My code below:

vector<int> twoSum(vector<int>& nums, int target) {
    
    for (int i = 0;i<nums.size();i++){
        int ans;
        ans = nums[i]+nums[i+1];
        if(ans == target)
            return nums[] {i,i+1};
        else{
            for(int j = i+1;j<size() - i; j++){
                int ans1;
                ans1 = nums[j] == nums[j+1];
                i++;
                if(ans1 == target)
                    return nums[] {j,j+1};
            }
        }
    }
}

                      

I am not too sure how I can return the index number of the array, so that was my best go at it.

  • What were you expecting with this line: `return nums[] {i, i+1};`? Also please explain what "two sum" is. – Ranoiaetep Sep 19 '20 at 06:52
  • 3
    I think if you would wrote `new int[]{j, j+1}`. I would say why are you trying to write java when you are at c++. You can easily do `return {j, j+1}`. – bilginyuksel Sep 19 '20 at 06:55
  • 1
    @salvador-soto Not only your syntax, but your logic is also wrong. It's nowhere mentioned in the [problem](https://leetcode.com/problems/two-sum/) that the elements must be adjacent. – brc-dd Sep 19 '20 at 07:11
  • 2
    *I am getting this error when I try to compile and it is for my return statements* -- Which goes to show you that you cannot learn C++ properly from online websites like Leetcode. The questions are meant for persons who know the language well enough to not even have to ask about basic syntax. Second, the solution is inefficient -- note that these websites ask questions where there is always an "easy" answer, but the problem with the easy answer is that it is more often than not, is highly inefficient, thus causing time out errors. – PaulMcKenzie Sep 19 '20 at 07:16
  • 1
    The comment section is for comments, so I commented. That is what the comment section section is for. Second, what I mentioned is exactly what you should be aware of -- the questions on LeetCode and on any of these competitive programming websites have easy answers that usually time-out, and then you really have to come up with efficient, and sometimes very difficult solutions. Is there anything wrong in pointing that out to you? Last, you could have gotten the usual response from someone else to "read books" -- did I do that? No. – PaulMcKenzie Sep 19 '20 at 07:43
  • @PaulMcKenzie Listen, I think I just took it the wrong way. I appreciate your advice for what it is regardless. I know it wasn't a personal attack, I just took it that way. Without divulging into personal matters, I have been feeling incompetent when it came to C++(trying to do things I don't have enough knowledge in yet really, i.e algorithms etc) recently and this little problem made me feel like I was starting to head in the right direction regardless if what I originally thought was wrong. And I just took your comment and connected it to how I was feeling and that was my fault. – salvador-soto Sep 19 '20 at 07:50
  • 1
    No problem -- we get a lot of LeetCode and Hackerrank questions here, and more times than not, it comes to inefficiency (i.e. time-out errors). That's why before you submitted your solution, I like to warn newbies about sites like those and the types of questions they ask. Nothing wrong with those questions, just that I wouldn't want to have you waste your time with any solution, and then find out the solution isn't good enough. – PaulMcKenzie Sep 19 '20 at 07:53
  • @PaulMcKenzie With your extensive amount of knowledge are there any books(or anything else) you can recommend someone in my position that knows some basic c++ but obviously have a lot of gaps in my knowledge. Anything helps, I really just want to learn :/ – salvador-soto Sep 19 '20 at 07:58
  • [Try this](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – PaulMcKenzie Sep 19 '20 at 08:01

2 Answers2

2

Seems like you were trying to create a vector with two elements in it, i and i+1. However the syntax you were trying to use, not sure where you got that from, but it was wrong.

To create a vector with elements defined, you would do something like:

std::vector<elementType> vec {element1, element2... };

So in your case, you would probably do:

return std::vector<int> {i, i+1};
Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
0

Okay this here is what was Accepted on Leet Code:

//Brute force for c++ -> O(n^2).
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
    vector<int> arr(2);
    for(int i = 0;i<nums.size();i++){
        int ans;
        ans = nums[i]+nums[i+1];
        if(ans == target)
        arr[0]=i;
        arr[1]=i+1;
        return arr;
        else{
            for(int j = i+1;j<nums.size() - i; j++){
                int ans1;
                ans1 = nums[j] == nums[j+1];
                i++;
                if(ans1 == target){
                    arr[0]= j;
                    arr[1]= j+1;
                }
                    
             }
         }
       }
    return arr;
    }
};

I know it is not optimized at all but I am just doing these easy questions to get in the process of thinking algorithmically and I am actually really proud of myself regardless because I had the actual idea down and was only missing some but I got the solution down in essence. Thanks to everyone who tried to help me out and for giving me the right ideas and ways to go about it :)

Okay this solution kind of (really) sucks and doesn't really work but I figure ill keep it here and people can learn from what not to do and how you should not approach this problem.

  • 1
    To be honest, this is not the way it should be done. The way this problem is solved is to traverse the array once, and on each iteration, subtract the array element from the goal amount -- then you see if that result has been "seen already". If it has, then that's the pair. If you knew nothing about hash tables, that intuitively is what you should have thought of. What you have now is really, to be honest, very convoluted. – PaulMcKenzie Sep 19 '20 at 07:35
  • Also note that the solution would require a table of "already seen" items so you can search the table-- that's where the hash table, or some sort of structure comes into play. That's how you get into learning these sorts of things -- just intuitively, you know you need some sort of structure to store and search for values. How to implement the table -- that's another story. – PaulMcKenzie Sep 19 '20 at 07:57