猿问

两个和类。

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;

}

}

我不确定该类如何能够获取哈希映射中的数字并告诉我们是否可以将 2 个数字相加以创建另一个数字。具体来说,我不明白 find 布尔值是如何工作的,或者为什么 add void 以它的方式以及出于什么原因将数字放入哈希映射中。这个类实际上应该做的是使用 add 函数将项目添加到哈希映射,然后使用 find 来确定是否可以使用任何两个整数来加和目标。


杨魅力
浏览 140回答 1
1回答

慕慕森

请参阅下面代码中的注释。public class TwoSum {&nbsp; &nbsp; // create a hashmap to contain the NUMBER added and the COUNT of that number&nbsp; &nbsp; private HashMap<Integer, Integer> elements = new HashMap<Integer, Integer>();&nbsp; &nbsp; public void add(int number) {&nbsp; &nbsp; &nbsp; &nbsp; // does the hashmap have the NUMBER as a key&nbsp; &nbsp; &nbsp; &nbsp; if (elements.containsKey(number)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get the COUNT of the NUMBER and increment it by 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // and update the hashmap&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elements.put(number, elements.get(number) + 1);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the NUMBER doesn't exist in the hashmap,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // so add it and set the COUNT to 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elements.put(number, 1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public boolean find(int value) {&nbsp; &nbsp; &nbsp; &nbsp; // Loop through the NUMBERS (which are keys in the hashmap&nbsp; &nbsp; &nbsp; &nbsp; for (Integer i : elements.keySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // subtract the NUMBER (i) from the VALUE then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // all we have to do is look for the TARGET in the hashmap&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int target = value - i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // start looking for the TARGET&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (elements.containsKey(target)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If we made it here, we found a match&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if I == TARGET, then there has to be a COUNT of at least 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // for example if VALUE = 6 and I = 3 then TARGET also = 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // so the COUNT of 3s in the hashmap has to be at least 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if the COUNT is not >= 2 then we jump to the next I&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == target && elements.get(target) < 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue; // jump to next I&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true; // we found a match to TARGET so we can exit&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false; // no matches for TARGET&nbsp;&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答