# 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; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up