Two Sum III - Data structure design(leetcode170)

Design and implement a TwoSum class. It should support the following operations: add and find.

add - Add the number to an internal data structure.
find - Find if there exists any pair of numbers which sum is equal to the value.

For example,

add(1); add(3); add(5);
find(4) -> true
find(7) -> false

A simpler approach is to store each input into a hash table with the input as key and its count as value. To find if a pair sum exists, just iterate through the hash table in O(n) runtime. Make sure you are able to handle duplicates correctly.

 1 public class TwoSum{
 2     //Firstly, we create a hashmap
 3     Map<Integer, Integer> hm = new HashMap<>();
 4
 5     public void add(int number){
 6         int count = hm.containsKey(number) ? hm.get(number): 0;
 7         hm.put(number, count +1);
 8    }
 9
10    public boolean find(int value){
11         //The Map.Entry interface enables you to work with a map entry
12         for(Map.Entry<Integer, Integer> entry: hm.entrySet()){
13             int num = entry.getKey();
14             int r = value - num;
15             if(r == num){
16                 // For duplicates, ensure there are at least two individual numbers
17                 if(entry.getValue() >= 2) return true;
18              }else if(hm.containsKey(r)){
19                 return true;
20             }
21         }
22         return false;
23   }
24 }                    
时间: 2024-10-08 09:30:05

Two Sum III - Data structure design(leetcode170)的相关文章

leetcode 170: Two Sum III - Data structure design

Two Sum III - Data structure design Total Accepted: 311 Total Submissions: 1345 Design and implement a TwoSum class. It should support the following operations:add and find. add - Add the number to an internal data structure. find - Find if there exi

170. Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); ad

LeetCode-Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure. find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); a

Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); ad

leetcode3 Two Sum III – Data structure design

Question: Design and implement a TwoSum class. It should support the following operations: add and find. add(input) – Add the number input to an internal data structure. find(value) – Find if there exists any pair of numbers which sum is equal to the

[LC] 170. Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. Example 1: add(1); add(

[LeetCode][JavaScript]Add and Search Word - Data structure design

Add and Search Word - Data structure design Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or 

【LeetCode-面试算法经典-Java实现】【216-Combination Sum III (组合数的和)】

[216-Combination Sum III (组合数的和)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination

【LeetCode】211. Add and Search Word - Data structure design

Add and Search Word - Data structure design Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or