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 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

[分析]
HASH table O(N)存, O(1) 取 
public class TwoSum {
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();

	public void add(int number) {
	    if(map.containsKey(number)) {
	        map.put(number, map.get(number) + 1);
	    } else {
	        map.put(number, 1);
	    }
	}

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

时间: 2024-12-14 18:09:52

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

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

[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-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

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

[LeetCode] 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 .. A . means it can represent any one letter

Leetcode: 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 .. A . means it can represent any one letter

LeetCode——Add and Search Word - Data structure design

Description: 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 .. A . means it can represent a