See C++primer Plus look bored, the first time do Leetcode practice, originally want to do two-dimensional vector, results always pass, check the reason, must use one-dimensional ...
One-dimensional answers:
Class Solution {
Public
Vector<int> twosum (vector<int>& nums, int target)
{
int cout = Nums.size (); Get the size of the vector
vector<int>ret; Defining vectors
for (int i = 0; i<cout; i++)
{
for (int j = i + 1; j<cout; J + +)
{
if ((Nums[i] + nums[j]) = = target)
{
Ret.push_back (i); Get the location of the target value
Ret.push_back (j);
}
}
}
return ret;
}
};
Two-dimensional answer:
Class Solution {
Public:vector<vector<int>> twosum (vector<int>& nums, int target)
{int cout = Nums.size (); Get the size of a two-dimensional vector
Vector<vector<int>>ret (cout); Defining two-dimensional vectors
for (int i = 0; I <cout; i++)
Ret[i].resize (2); Initialize two-dimensional vectors
int i0 = 0;
for (int i = 0; i<cout; i++)
{for (int j = i + 1; j<cout; J + +)
{if ((Nums[i] + nums[j]) = = target)
{Ret[i0][0] = i; Get the location of the target value
Ret[i0][1] = j;
i0++;
}
}
}
Ret.resize (I0 + 1); Keep only the useful parts
for (int i = 0; i < I0; i++)
std:: cout << ret[i][0] << "," << ret[i][1] << Std::endl;
return ret;
}
};
Leetcode gives a number A and a vector B, finding out that the 2 numbers in the vector b are added equal to a, and the position of the two numbers in the vector is output