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); add(3); add(5);
find(4) -> true
find(7) -> false
public class TwoSum {

    HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();

    // Add the number to an internal data structure.
    public void add(int number)
    {
        if(map.containsKey(number)) map.put(number, map.get(number)+1);
        else map.put(number,1);

    }

    // Find if there exists any pair of numbers which sum is equal to the value.
    public boolean find(int value)
    {
        for(int key:map.keySet())
        {
            int num1 = key;
            int num2 = value - num1;
            if(num1==num2&&map.get(num1)>1) return true;
            else if(num1!=num2&&map.containsKey(num2)) return true;
        }
        return false;
    }
}
时间: 2024-12-28 01:16:18

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

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(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); 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】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 

[LintCode] Two Sum - 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 add(1); add(3);