登录

c++ - LeetCode刷题在vs2015运行正确,网站一直报错?

题目的意思是:

给定一个保存int的vector,然后再给定一个int值target,从vector里边找出两个int值相加等于target。返回这两个值的下标。
e.g.输入[3,2,4] 6  输出应为[1,2]

代码如下:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        for(int i=0;i<nums.size();++i){
            for(int j=i+1;j<nums.size();++j){
                if(nums[i]+nums[j]==target)
                return {i,j};
            }
        }
    }
};

明明没错啊,,,,,我在vs2015上边跑也能出来结果。但是网站一直报错
Line 10: control reaches end of non-void function [-Werror=return-type]
哪位前辈知道是为嘛啊。。。

# C++
黄舟黄舟2049 天前459 次浏览

全部回复(1) 我要回复

  • PHP中文网

    PHP中文网2017-04-17 15:01:06

    因為gcc覺得你的control flow 有問題。
    因為twoSum的return type 是vector<int>,但如果沒有答案那twoSum就不會return。

    回复
    0
  • 取消回复发送