---
# System prepended metadata

title: 0170. Two Sum III - Data structure design
tags: [Leetcode, HashMap]

---

# 0170. Two Sum III - Data structure design
###### tags: `Leetcode` `HashMap`
Link: https://leetcode.com/problems/two-sum-iii-data-structure-design/
## 思路
和two sum类似
要注意remain和num相等的情况的处理
## Code
```java=
class TwoSum {
    Map<Integer, Integer> map;
    public TwoSum() {
        map = new HashMap<>();
    }
    
    public void add(int number) {
        map.put(number, map.getOrDefault(number, 0)+1);
    }
    
    public boolean find(int value) {
        for(int num:map.keySet()){
            int remain = value-num;
            if(num!=remain && map.containsKey(remain)) return true;
            else if(num==remain && map.get(num)>1) return true;
        }
        return false;
    }
}
```