Unique doesn't work on vector<vector<int>>?


  • 0
    D

    Hi,

    I used backtracking to solve this problem and tried unique on vector<vector<int>> res, but it didn't work.

    But isn't it supposed to work on vector<vector<int>> ? Thanks
    http://stackoverflow.com/questions/3773015/using-unique-on-a-vector-of-vectors-in-c

    class Solution {
    public:
        vector<vector<int> > combinationSum2(vector<int> &num, int target) {
            vector<vector<int>> res;
            int n=num.size();
            sort(num.begin(), num.end());
            if (n==0||num[0]>target)
               return res;
            vector<int> r;
            dfs(num, res,r, 0, target,0);
            unique(res.begin(),res.end());
            return res;
        }
        
        void dfs(vector<int> &n, vector<vector<int>> &res, vector<int> &r, int sum, int t, int start)
        {
            if (sum==t)
            {
                res.push_back(r);
                return;
            }
            else
            {
                int ns=n.size();
                for (int i=start; i<ns; i++)
                {
                    if (n[i]+sum>t)
                       return;
                    r.push_back(n[i]);
                    dfs(n,res,r,sum+n[i],t,i+1);
                    r.pop_back();
                }
            }
        }
    };

  • 0
    K

    You should sort the result at first.


Log in to reply
 

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