从地图中获取两个元素,它们的总和等于所需的数字

发布于 01-17 21:58 字数 125 浏览 1 评论 0原文

我正在尝试找到一种方法,从地图中获取前两个元素,其价值结合为我带来了理想的总和。 我当时想到了一个解决方案,该解决方案结合了2个地图,其中第二张地图的键是目标编号减去第一个地图的输入值。 我迷路了,不确定我缺少什么。 我在这里想念什么?

I am trying to find a way to get the first 2 elements from a map which their value combined gives me a desired sum.
I was thinking of a solution which combined 2 maps where the key of the second map is the reminder of the target number minus the value of the entry of the first map.
I am lost and not sure what I am missing.
What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情癖2025-01-24 21:58:25

我建议介绍一个新的映射resutderDoitem,通过所有相关项目进行循环,并将其提醒添加到映射中,作为键和项目密钥作为值,

然后迭代secoodantItems找到价格与其他提醒相匹配
还要检查resutderToItem.get(entry.getValue())。等于(entry.getKey())) (如果值为50提示也为50),此防止将同一项目再次添加到项目

private static List<String> getTwoItemsWhichSumTo100(Map<String, Integer> items, int target) {

    Map<String, Integer> relevantItems = getRelevantItems(items, target);
    Map<Integer, String> remainderToItem = new HashMap<>();
    List<String> itemsThatCanBeBought = new ArrayList<>();

    for (Map.Entry<String, Integer> entry : relevantItems.entrySet()) {
        int remainder = target - entry.getValue();
        remainderToItem.put(remainder, entry.getKey());
    }

    for (Map.Entry<String, Integer> entry : relevantItems.entrySet()) {

        if (remainderToItem.containsKey(entry.getValue()) && !remainderToItem.get(entry.getValue()).equals(entry.getKey())) {
            itemsThatCanBeBought.add(entry.getKey());
            itemsThatCanBeBought.add(remainderToItem.get(entry.getValue()));
            return itemsThatCanBeBought;
        }
    }
    return itemsThatCanBeBought;
}

private static Map<String, Integer> getRelevantItems(Map<String, Integer> items, int target) {
    Map<String, Integer> relevantItems = new HashMap<>();
    for (Map.Entry<String, Integer> entry : items.entrySet()) {
        if (entry.getValue() < target) relevantItems.put(entry.getKey(), entry.getValue());
    }
    return relevantItems;
}

I would suggest introducing a new Map remainderToItem, looping through all relevant items and adding their reminder to the Map as key and item key as the value

then iterate the relevantItems Map again to find the price is matching with some other reminder
Also check remainderToItem.get(entry.getValue()).equals(entry.getKey())) (in case the value is 50 reminder is also 50), this prevents adding same item again to itemsThatCanBeBought

private static List<String> getTwoItemsWhichSumTo100(Map<String, Integer> items, int target) {

    Map<String, Integer> relevantItems = getRelevantItems(items, target);
    Map<Integer, String> remainderToItem = new HashMap<>();
    List<String> itemsThatCanBeBought = new ArrayList<>();

    for (Map.Entry<String, Integer> entry : relevantItems.entrySet()) {
        int remainder = target - entry.getValue();
        remainderToItem.put(remainder, entry.getKey());
    }

    for (Map.Entry<String, Integer> entry : relevantItems.entrySet()) {

        if (remainderToItem.containsKey(entry.getValue()) && !remainderToItem.get(entry.getValue()).equals(entry.getKey())) {
            itemsThatCanBeBought.add(entry.getKey());
            itemsThatCanBeBought.add(remainderToItem.get(entry.getValue()));
            return itemsThatCanBeBought;
        }
    }
    return itemsThatCanBeBought;
}

private static Map<String, Integer> getRelevantItems(Map<String, Integer> items, int target) {
    Map<String, Integer> relevantItems = new HashMap<>();
    for (Map.Entry<String, Integer> entry : items.entrySet()) {
        if (entry.getValue() < target) relevantItems.put(entry.getKey(), entry.getValue());
    }
    return relevantItems;
}
陈年往事2025-01-24 21:58:25

您面临的问题是 int 是 value ,而不是 key 。
所以 pairOfItems.containsKey(remainder) 不应编译。
幸运的是,Map 还有 containsValue() 方法。只要没有最低性能要求,就可以解决您的问题。
...而且你不需要第二张地图。你可以问items.containsValue(reminder)

the problem you're facing is that the int is the value , not the key.
so pairOfItems.containsKey(remainder) should not compile.
luckily for you, Map also has containsValue() method. as long as there is no requirement for minimum performance, that should solve your problem.
...and you don't need a 2nd map. you can just ask items.containsValue(reminder)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文