electro177's blog

By electro177, history, 3 years ago, In English

hello friends

Is there any way or stl trick to convert vector to vector but there should'nt be any equal elements eg)

[[1,1,6],[1,2,5],[1,2,5],[1,7],[1,7],[2,6]]

to

[[1,1,6],[1,2,5],[1,7],[2,6]]

for vector there exist a std::unique but for this case what to do :)

  • Vote: I like it
  • +3
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it +18 Vote: I do not like it

Sorting and using std::unique works here as well.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it -63 Vote: I do not like it

    for this problem ~~~~~

    define all(x) (x).begin(), (x).end()

    vector<vector> ans; int n; class Solution {

    public:
    void solve(int index ,int target ,vector<int>& candidates,vector<int> &ds){
    
        if(index==n){
    
            if(target==0){
                vector<int> temp=ds;
                 sort(all(temp));
                ans.push_back(temp);
            }
            return;
        }
    
        if(target>=candidates[index]){
            ds.push_back(candidates[index]);
    
            solve(index+1,target-candidates[index],candidates,ds);
            ds.pop_back();
    
        }
    
         solve(index+1,target,candidates,ds);
    }
    
    
    public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
       sort(all(candidates));         
      ans.clear(); 
        n=target;
        vector<int> ds;
        solve(0,target,candidates,ds);
        sort(all(ans));

    ans.resize(unique(all(ans)) — ans.begin()); return ans; }

    }; ~~~~~ for this problem i did that and it worked on a test case but gave runtime error for other test

    ~~~~~

    ==29==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000007d4 at pc 0x000000348638 bp 0x7fffa9e75030 sp 0x7fffa9e75028 READ of size 4 at 0x6020000007d4 thread T0 #4 0x7fb195bd70b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) 0x6020000007d4 is located 0 bytes to the right of 4-byte region [0x6020000007d0,0x6020000007d4) allocated by thread T0 here: #6 0x7fb195bd70b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) Shadow bytes around the buggy address: 0x0c047fff80a0: fa fa fd fa fa fa fd fa fa fa fd fd fa fa fd fa 0x0c047fff80b0: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa 0x0c047fff80c0: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa 0x0c047fff80d0: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa 0x0c047fff80e0: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa =>0x0c047fff80f0: fa fa fd fa fa fa fd fa fa fa[04]fa fa fa 04 fa 0x0c047fff8100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8110: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8120: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8130: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c047fff8140: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==29==ABORTING

    ~~~~~

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Please use spoiler tags for the code and the error.