Java 两类和 公共类TwoSum{ 私有HashMap元素=新HashMap(); 公共无效添加(整数){ if(元素。容器(编号)){ elements.put(number,elements.get(number)+1); }否则{ 元素。put(数字,1); } } 公共布尔查找(int值){ for(整数i:elements.keySet()){ int目标=值-i; if(元素。容器(目标)){ if(i==target&&elements.get(target)=2,那么我们跳到下一个I if(i==target&&elements.get(target)

Java 两类和 公共类TwoSum{ 私有HashMap元素=新HashMap(); 公共无效添加(整数){ if(元素。容器(编号)){ elements.put(number,elements.get(number)+1); }否则{ 元素。put(数字,1); } } 公共布尔查找(int值){ for(整数i:elements.keySet()){ int目标=值-i; if(元素。容器(目标)){ if(i==target&&elements.get(target)=2,那么我们跳到下一个I if(i==target&&elements.get(target),java,arrays,Java,Arrays,我不确定该类如何能够获取哈希映射中的数字,并告诉我们是否可以将两个数字相加以创建另一个数字。具体地说,我不理解find boolean是如何工作的,也不理解addvoid为什么会以这种方式将数字放入哈希映射,以及原因是什么。实际上,这个类应该做的是使用add函数将项目添加到散列映射中,然后使用find来确定是否可以使用任意两个整数向目标相加 请参见下面代码中的注释 public class TwoSum { private HashMap<Integer, Integer> elem

我不确定该类如何能够获取哈希映射中的数字,并告诉我们是否可以将两个数字相加以创建另一个数字。具体地说,我不理解find boolean是如何工作的,也不理解addvoid为什么会以这种方式将数字放入哈希映射,以及原因是什么。实际上,这个类应该做的是使用add函数将项目添加到散列映射中,然后使用find来确定是否可以使用任意两个整数向目标相加

请参见下面代码中的注释

public class TwoSum {
private HashMap<Integer, Integer> elements = new HashMap<Integer, Integer>();

public void add(int number) {
    if (elements.containsKey(number)) {
        elements.put(number, elements.get(number) + 1);
    } else {
        elements.put(number, 1);
    }
}

public boolean find(int value) {
    for (Integer i : elements.keySet()) {
        int target = value - i;
        if (elements.containsKey(target)) {
            if (i == target && elements.get(target) < 2) {
                continue;
            }
            return true;
        }
    }
    return false;
}
}
公共类TwoSum{
//创建一个hashmap来包含添加的数字和该数字的计数
私有HashMap元素=新HashMap();
公共无效添加(整数){
//hashmap是否将数字作为键
if(元素。容器(编号)){
//获取数字的计数并将其递增1
//并更新hashmap
elements.put(number,elements.get(number)+1);
}否则{
//hashmap中不存在该数字,
//因此,添加它并将计数设置为1
元素。put(数字,1);
}
}
公共布尔查找(int值){
//循环遍历数字(它们是hashmap中的键)
for(整数i:elements.keySet()){
//从值中减去数字(i),然后
//我们所要做的就是在hashmap中查找目标
int目标=值-i;
//开始寻找目标
if(元素。容器(目标)){
//如果我们能在这里找到匹配的
//如果I==TARGET,那么必须有至少2个计数
//例如,如果值=6且I=3,则目标也=3
//所以hashmap中3的计数必须至少为2
//如果计数不是>=2,那么我们跳到下一个I
if(i==target&&elements.get(target)<2){
继续;//跳到下一个I
}
return true;//我们找到了与目标匹配的项,因此可以退出
}
}
返回false;//目标没有匹配项
}
}

那么你想在这里实现什么呢?你可以在edited上读到coder试图解决的问题。我想我说得很清楚,但我想我是在模糊我的缺点。所以,你写了两个方法。第一个返回void;第二个返回boolean。你的问题必须(至少)在FYI上进行审查,
add()
方法可以替换为
元素。merge(number,1,Integer::sum);
。哦,伙计,这现在很有意义。真不敢相信我以前不懂。谢谢你的解释。
public class TwoSum {
    // create a hashmap to contain the NUMBER added and the COUNT of that number
    private HashMap<Integer, Integer> elements = new HashMap<Integer, Integer>();

    public void add(int number) {
        // does the hashmap have the NUMBER as a key
        if (elements.containsKey(number)) {
            // get the COUNT of the NUMBER and increment it by 1
            // and update the hashmap
            elements.put(number, elements.get(number) + 1);
        } else {
            // the NUMBER doesn't exist in the hashmap,
            // so add it and set the COUNT to 1
            elements.put(number, 1);
        }
    }

    public boolean find(int value) {
        // Loop through the NUMBERS (which are keys in the hashmap
        for (Integer i : elements.keySet()) {
            // subtract the NUMBER (i) from the VALUE then
            // all we have to do is look for the TARGET in the hashmap
            int target = value - i;
            // start looking for the TARGET
            if (elements.containsKey(target)) {
                // If we made it here, we found a match
                // if I == TARGET, then there has to be a COUNT of at least 2
                // for example if VALUE = 6 and I = 3 then TARGET also = 3
                // so the COUNT of 3s in the hashmap has to be at least 2
                // if the COUNT is not >= 2 then we jump to the next I
                if (i == target && elements.get(target) < 2) {
                    continue; // jump to next I
                }
                return true; // we found a match to TARGET so we can exit
            }
        }
        return false; // no matches for TARGET 
    }
}