简体   繁体   中英

C++ Excess elements in scalar initializer

I have this assignment that i have to return 2 indices of the numbers in nums that add up to the int target the output is supposed to look like this: [1,2].

But in the return statement i get the error what am i doing wrong?

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

You choose to use vector<int> as return value, so you won't need to use new .

Remove extra things.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM