I'm trying to solve this simple leetcode question. I'm pretty sure the approach is correct but for some reason, I get the following error and I'm not able to figure it out.
Error:
Line 9: Char 16: error: no viable conversion from returned value of type 'int [nums.size()]' to function return type 'vector' return ans;
Here's my code:
class Solution {
public:
vector<int> shuffle(vector<int>& nums, int n) {
int ans[nums.size()];
for (int i=0,j=n;i<n;i ,j ) {
ans[i*2] = nums[i];
ans[1 i*2] = nums[j];
}
return ans;
}
};
Problem link: https://leetcode.com/problems/shuffle-the-array/
A little explanation would be appreciated! Thanks
CodePudding user response:
ans
is an array, not a std::vector
. There is no implicit conversion of an array to a std::vector
. To fix your code, just declare ans
as:
std::vector<int> ans(nums.size());