【问题标题】:How to remove duplicate items from vector<vector<int>>如何从 vector<vector<int>> 中删除重复项
【发布时间】:2021-09-12 09:36:52
【问题描述】:

我正在尝试解决Combination II,这类似于硬币兑换问题,具有独特的组合,硬币没有无限重复。

例如:coins: {1,2,4}, amount = 3 {1,1,1,1}{1,1,2} 不允许,因为硬币 1 的频率是一次。(单个硬币只能使用一次)

{1,1,4} ,amount=3 {1,1,2} --> 将被允许,因为这两个来自两个不同的 1 硬币。

工作代码: https://ide.geeksforgeeks.org/5fjEmsWXUr

class Solution {
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        
        sort(candidates.begin(), candidates.end());
        vector <vector <vector <int>>> dp(target+1);
        dp[0] = {{}};
        //traverse throuugh coins
        for (int i=0; i<candidates.size()-1; i++){
            // traverse through DP (amount -> 0 inclusive)
            for(int j = target; j>=candidates[i]; j--){ // just reverse it start with target and use target-coin amount to fill. In this way we are just adding current number not repeating the same no. infinite times as done in j = candidates[i] to target+1; 
                // order repetition will be there. 
                for(auto v: dp[j-candidates[i]]){
                    v.push_back(candidates[i]);                    
                    dp[j].push_back(v);
                } 
            }
        }
        return dp[target];        
    }
};

您的意见

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

输出

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

预期

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

就我而言:

[1,2,5] 出现了两次,[1,7] 也出现了。

我想删除这些重复项。 我知道我们可以使用set 来存储它们。但是我在使用set 和修改函数返回类型时遇到了问题。

【问题讨论】:

  • 如果您使用 std::set 而不是 std::vector 它不能包含重复项。你遇到了什么问题?
  • @463035818_is_not_a_number:我知道我必须使用 set。只是我不习惯使用 set。还必须在代码中进行修改,我得到了错误:'(。
  • set 可能是最有效的,另一种方法是如果它已经存在则不添加任何内容。例如使用 std::find_if。 (虽然这个例子可能会变得太慢)
  • 函数必须返回一个vector&lt;vector&lt;int&gt;&gt; ?然后使用set&lt;vector&lt;int&gt;&gt;,只有当你返回它时,你才会创建向量
  • “必须在代码中进行修改”确保你这样做。不更改代码就无法更改代码。 “我也遇到了错误”请显示损坏的代码和错误

标签: c++ c++14


【解决方案1】:

使用std::set。实际上就是用set 替换内部的vector

class Solution {
public:
    std::vector<std::vector<int>> combinationSum2(std::vector<int>& candidates, int target) {

        std::sort(candidates.begin(), candidates.end());
        std::vector<std::set<std::vector<int>>> dp(target+1);
        dp[0] = {{}};
        //traverse throuugh coins
        for (int i=0; i<candidates.size()-1; i++){
            // traverse through DP (amount -> 0 inclusive)
            for(int j = target; j>=candidates[i]; j--){ // just reverse it start with target and use target-coin amount to fill. In this way we are just adding current number not repeating the same no. infinite times as done in j = candidates[i] to target+1; 
                // order repetition will be there. 
                for(auto v: dp[j-candidates[i]]){
                    v.push_back(candidates[i]);                    
                    dp[j].insert(v);
                } 
            }
        }
        return {dp[target].begin(),dp[target].end()};        
    }
};

仅当返回向量的向量时,您需要转换集合并且set 不是push_back,而是insert

【讨论】:

  • 当我尝试使用 set.我在dp[target] 中遇到了返回类型的问题;最后一行{dp[target].begin(),dp[target].end()} ----> vector&lt;vector&lt;int&gt;&gt;
  • @Pygirl 我假设您无法更改返回类型
  • @Pygirl 不确定你的意思。在这里工作正常:godbolt.org/z/En7Tf49re
  • @Pygirl 你的代码中没有liststd::vector 的构造函数采用一系列元素。
  • @Pygirl 小心术语。此代码中没有强制转换。这是你习惯的问题。我觉得很痛苦,在 python 中,你永远不知道某个东西是什么类型,而且你几乎无法控制它
猜你喜欢
  • 2019-07-11
  • 2011-07-20
  • 2011-05-25
  • 2017-08-11
  • 2020-12-26
  • 2021-11-07
  • 2014-05-24
  • 2013-02-28
  • 1970-01-01
相关资源
最近更新 更多