Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What the class describes is about "two sum", which is correct and usable from the Leetcode website. Today, I want to present "two sum" by inputting a value by myself (such as the int main() part below). The expected output result is [0,1], but I still can't execute it after thinking for a long time. Beginners sincerely ask for advice, maybe you can also attach your writing so that I can learn, thank you very much.

C++
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

class Solution
{
public:
    vector<int> twoSum(vector<int> &nums, int target)

    {
        unordered_map<int, int> indies;
        for (int i = 0; i < nums.size(); ++i)
            indies[nums[i]] = i;

        for (int i = 0; i < nums.size(); ++i)
        {
            int left = target - nums[i];
            if (indies.count(left) && indies[left] != i) 
            {
                return {i, indies[left]};
            }
        }
        return {};
    }
};

int maim()
{
    Solution twotwo;
    vector<int> number = {2, 7, 11, 15};
    int tg = 9;
    cout << twotwo.twoSum(number, tg);
}


What I have tried:

I want to present "two sum" by inputting a value by myself (such as the int main() part below). The expected output result is [0,1], but I still can't execute it after thinking for a long time.
Posted
Updated 1-Sep-20 2:17am
v3

The function returns a vector of integers, but cout isn't directly able to print them. There are (at least) two alternative solutions:

  • Iteratively print the vector items in the main function:
C++
int main()
{
    Solution twotwo;
    vector<int> number = {2, 7, 11, 15};
    int tg = 9;

    vector<int> result = twotwo.twoSum(number, tg);


    // print the resulting vector

    cout << "[";
    for (size_t n = 0; n < result.size(); ++n)
    {
      cout << result[n];
      if ( n != result.size()-1)
        cout << ",";
    }
    cout << "]" << endl;
}

or..
  • since it is a wonderful occasion for overloading the insertion operator <<, do exactly that:
C++
ostream & operator << (ostream & os, const vector <int > & v)
{
  os << "[";

  for ( size_t n = 0; n < v.size(); ++n)
  {
    os << v[n];
    if ( n != (v.size() - 1) )
    os << ',';
  }

  os << "]";

  return os;
}


int main()
{
    Solution twotwo;
    vector<int> number = {2, 7, 11, 15};
    int tg = 9;
    cout << twotwo.twoSum(number, tg) << endl;
}
 
Share this answer
 
v2
Comments
Amy Zhou 1-Sep-20 23:12pm    
Thank you very much. The current situation is that the print result problem has been solved. I can use the online compiler to execute it, but the Visual Studio 2019 for Mac I use has the following problems:

warning: generalized initializer lists are a C++11 extension [-Wc++11-extensions]
return {i,indies[left]};

error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
return {i,indies[left]};

warning: generalized initializer lists are a C++11 extension [-Wc++11-extensions]
return {};

error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
return {};

error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
vector<int> number = {2, 7, 11, 15};

I had read many related solutions, such as changing to "-std=c++11" and so on, but there is still no way to solve the error. Could you please teach me how to solve it...
CPallini 2-Sep-20 2:01am    
I suggest you to use
std=c++latest
See
https://docs.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=vs-2019

Amy Zhou 2-Sep-20 3:59am    
I'm sorry, I would like to ask about the place to determine the change. Is it under my project workspace, and then in the task.json below, the place corresponding to "args" is changed to "-std=c++latest"?
CPallini 2-Sep-20 4:32am    
You may easily set such property in the Visual Sudio Ide. See the instructions at the bottom of the page I linked in the previous comment.
First, fix your function name: "maim" is not the same as "main"

Secondly, you can't print a vector directly. This will help: How to print out the contents of a vector in C++?[^]
 
Share this answer
 
Comments
Amy Zhou 1-Sep-20 23:12pm    
Thank you very much. The current situation is that the print result problem has been solved. I can use the online compiler to execute it, but the Visual Studio 2019 for Mac I use has the following problems:

warning: generalized initializer lists are a C++11 extension [-Wc++11-extensions]
return {i,indies[left]};

error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
return {i,indies[left]};

warning: generalized initializer lists are a C++11 extension [-Wc++11-extensions]
return {};

error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
return {};

error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
vector<int> number = {2, 7, 11, 15};

I had read many related solutions, such as changing to "-std=c++11" and so on, but there is still no way to solve the error. Could you please teach me how to solve it...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900