简体   繁体   English

C++ 标量初始值设定项中的多余元素

[英]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].我有这个任务,我必须返回 nums 中数字的 2 个索引,这些索引加起来为 int 目标,输出应该是这样的:[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 .您选择使用vector<int>作为返回值,因此您不需要使用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};
                }
            }
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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