Skip to content

Latest commit

 

History

History
36 lines (29 loc) · 784 Bytes

Question_170.md

File metadata and controls

36 lines (29 loc) · 784 Bytes

LeetCode Records - Question 170 Two Sum III - Data structure design

Attempt 1: Use a HashMap

class TwoSum {

    private final Map<Integer, Integer> map;

    public TwoSum() {
        map = new HashMap<>();
    }

    public void add(int number) {
        map.merge(number, 1, Integer::sum);
    }

    public boolean find(int value) {
        for (int key : map.keySet()) {
            int target = value - key;
            if (key != target) {
                if (map.containsKey(target)) {
                    return true;
                }
            } else if (map.get(key) > 1) {
                return true;
            }
        }

        return false;
    }
}
  • Runtime: 80 ms (Beats: 74.87%)
  • Memory: 52.49 MB (Beats: 52.34%)