class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> result;
for (vector<int>::iterator it = nums.begin(); it != nums.end(); it++)
{
for (vector<int>::iterator ij = ++nums.begin(); ij != nums.end(); ij++)
{
if (*it + *ij == target)
{
vector<int>::iterator pos1 = it;
vector<int>::iterator pos2 = ij;
result.push_back(*it);
result.push_back(*ij);
return result;
}
}
}
}
};
我在VS中代码都可以运行,也符合题目要求
报错提示是
Line 21: Char 2: error: non-void function does not return a value in all control paths [-Werror,-Wreturn-type]
}
^
1 error generated.
意思是这个函数没有一个肯定能执行到的return语句。你代码中只有一个return语句,如果if条件一直不满足,就执行不到return语句了。一般在所有for循环结束后,要加一个return语句,比如在22行前加一句return result;就好了
必须要一个 return的语句,比如在你代码上加上return result 或 return {}