Couldn't figure out runtime error

  • 0

    Simply changing vec[index+1] = i; to vec.push_back(i); fixed the runtime error.

    I got runtime error: Last executed input: [1,2], 3
    I double checked my code but still couldn't figure out what's wrong. Any help would be appreciated.
    My solution is as below:

    class Solution {
        vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
            vector<vector<int>> vecs;
            if(candidates.empty()) {
                return vecs;
            sort(candidates.begin(), candidates.end());
            vector<int> vec={0};
            helper(candidates, target, 0, 0, vec, vecs);
        void helper(vector<int> &can, int target, int sum, int index, vector<int> &vec, vector<vector<int>> &vecs) {//if no & for vec,
        //Input:[1], 2, Output:	[[1]], Expected:[[1,1]]
            if(target == sum) {
                vector<int> res;
                for(int i=1; i<vec.size(); ++i) {
            } else if (target> sum) {
                for(int i=vec[index]; i<can.size(); ++i) {
                    vec[index+1] = i;
                    helper(can, target, sum+can[i], index+1, vec, vecs);
            } else if (target<sum) {//if missing this: Runtime Error,Last executed input:	[2], 1

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.