[LeetCode] Two Sum III - Data Structure Design

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

Problem Description:

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

Well, the basic idea is simple: just store the added numbers in an internal data structure. When we are asked to find a value, we just iterate over all the numbers in the internal data structure. For each number num, if value - num is also in the data structurue and not at the same "position" as num, then return true. After iterating over all num and we have not returnred true, return false.

The key to an efficient implementation lies in what data structure to store the added numbers. Since there may be duplicate numbers, personally I think using an unordered_map is a nice choice. It uses num as key and its number of appearances as value. It enalbes both O(1) insertion and O(1) query, and thus O(1) for add and O(n) for find if there are n elements in total.

The code is as follows.

 1 class TwoSum {
 2 public:
 3     void add(int number) {
 4         data[number]++;
 5     }
 6 
 7     bool find(int value) {
 8         for (auto pr : data) {
 9             int first = pr.first, second = value - first;
10             if ((first != second && data.find(second) != data.end()) || (first == second && data[first] > 1))
11                 return true;
12         }
13         return false;
14     }
15 private:
16     unordered_map<int, int> data;
17 };

 

目录
相关文章
|
存储 缓存 算法
LeetCode刷题---Two Sum(一)
LeetCode刷题---Two Sum(一)
Leetcode 4. Median of Two Sorted Arrays
题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。 如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
44 0
|
存储 C++ Python
LeetCode刷题---Add Two Numbers(一)
LeetCode刷题---Add Two Numbers(一)
|
存储 算法 安全
LeetCode - #2 Add Two Numbers
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #2 Add Two Numbers
|
存储 算法 安全
LeetCode - #1 Two Sum
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #1 Two Sum
LeetCode 350. Intersection of Two Arrays II
给定两个数组,编写一个函数来计算它们的交集。
80 0
LeetCode 350. Intersection of Two Arrays II
|
Python
LeetCode 349. Intersection of Two Arrays
给定两个数组,编写一个函数来计算它们的交集。
77 0
LeetCode 349. Intersection of Two Arrays
LeetCode 160. Intersection of Two Linked Lists
编写一个程序,找到两个单链表相交的起始节点。
72 0
LeetCode 160. Intersection of Two Linked Lists
LeetCode 167 Two Sum II - Input array is sorted(输入已排序数组,求其中两个数的和等于给定的数)
给定一个有序数组和一个目标值 找出数组中两个成员,两者之和为目标值,并顺序输出
93 0
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists