-2

I'm trying to do this Leetcode challenge:

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

But I'm getting this error:

Line 10: Char 28: error: no viable conversion from returned value of type 'int [2]' to function return type 'vector'

On this statement:

return arrayresult;

Here is my code:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
    for (int i = 0; i < sizeof(nums); i++)
        for (int j = 0; j < sizeof(nums); j++)
            if (i != j){
                int result = nums[i] + nums[j];
                if (result == target){
                    int arrayresult[] = {i,j}; //Line 10
                    return arrayresult;
                }
            }
    }
};

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    `int arrayresult[]` <> `vector`!!! – paulsm4 Dec 27 '21 at 06:37
  • 2
    also `sizeof(nums)` is not the same as `nums.size()` – Passerby Dec 27 '21 at 06:38
  • Does this answer your question? [Arrays vs Vectors: Introductory Similarities and Differences](https://stackoverflow.com/questions/15079057/arrays-vs-vectors-introductory-similarities-and-differences) – paulsm4 Dec 27 '21 at 06:40
  • 2
    *I'm trying to do this Leetcode challenge* -- The questions asked on Leetcode assume you know the language you will be using to answer the questions, well enough to never make simple mistakes as you're making now. Leetcode and other similar websites are not designed to teach you how to write C++ code. Use proper C++ reading material (i.e. good peer-reviewed books) if you want to learn C++ properly. – PaulMcKenzie Dec 27 '21 at 06:45
  • I totally agree with Paul, sites like leetcode will teach you problem solving skills but also bad coding practices. If you can't get your hands on a book have a look here : https://www.learncpp.com/. – Pepijn Kramer Dec 27 '21 at 06:56
  • I think it is better to find your problem by debugging your code. it can improve your programming skill. – Saman Salehi Jan 04 '22 at 12:08

1 Answers1

0

Your code has two major problems.

First of all, twoSum() returns a std::vector<int> but you are returing a int[] type. Because int[] cannot be implicty convert to std::vector<int>, this will retrun an error.

Second, sizeof(num) returns how many bytes num occupies in memory, not how many elements are there. You should use the size() function.

Your code should be:

class Solution {
public:
    std::vector<int> twoSum(std::vector<int>& nums, int target) {
    for (int i = 0; i < num.size(); i++)
        for (int j = 0; j < num.size(); j++)
            if (i != j){
                int result = nums[i] + nums[j];
                if (result == target){
                    std::vector<int>arrayresult = {i,j};
                    return arrayresult;
                }
            }
    }
    return std::vector<int>{-1}; // return a dummy value
};