Iterative C++ solution by finding the last occurrence of the duplicated current element


  • 1
    R

    Iterative C++ solution by finding the last occurrence of the duplicated current element

     vector<vector<int>> v(1,vector<int>());
        sort(nums.begin(), nums.end());
        for (int i = 0; i < nums.size(); ++i) {
            vector<vector<int>> tv(move(v));
            for (int j = 0; j < tv.size(); ++j) {
                int k = i>0&&nums[i] == nums[i-1] ? last(tv[j],nums[i])+1:0;
                for (; k <= tv[0].size(); ++k) {
                    vector<int> tmp(tv[j]);
                    tmp.insert(tmp.begin()+k,nums[i]);
                    v.push_back(move(tmp));
                }
            }
        }
        return v;
    }
    //to find the last occurence of the duplicated element k
    inline int lastPre(vector<int> &v, int k) {
        for (int i = v.size()-1; i >=0; --i) {
            if (v[i] == k) return i;
        }
    }

Log in to reply
 

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