从地图中获取两个元素,它们的总和等于所需的数字
我正在尝试找到一种方法,从地图中获取前两个元素,其价值结合为我带来了理想的总和。 我当时想到了一个解决方案,该解决方案结合了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 技术交流群。

发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我建议介绍一个新的映射
resutderDoitem
,通过所有相关项目进行循环,并将其提醒添加到映射中,作为键和项目密钥作为值,然后迭代
secoodantItems
找到价格与其他提醒相匹配还要检查
resutderToItem.get(entry.getValue())。等于(entry.getKey()))
(如果值为50提示也为50),此防止将同一项目再次添加到项目
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 valuethen iterate the
relevantItems
Map again to find the price is matching with some other reminderAlso check
remainderToItem.get(entry.getValue()).equals(entry.getKey()))
(in case the value is 50 reminder is also 50), this prevents adding same item again toitemsThatCanBeBought