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 value.
For example,
add(1); add(3); add(5); find(4) : true; find(7) : false

Solution:
add – O(n) runtime, find – O(1) runtime, O(n2) space – Store pair sums in hash table:
We could store all possible pair sums into a hash table. The extra space needed is in the order of O(n2). You would also need an extra O(n) space to store the list of added numbers. Each add operation essentially go through the list and form new pair sums that go into the hash table. The find operation involves a single hash table lookup in O(1) runtime.This method is useful if the number of find operations far exceeds the number of add operations.

add – O(log n) runtime, find – O(n) runtime, O(n) space – Binary search + Two pointers:
Maintain a sorted array of numbers. Each add operation would need O(log n) time to insert it at the correct position using a modified binary search (See Question [48. Search Insert Position]). For find operation we could then apply the [Two pointers] approach in O(n) runtime.

add – O(1) runtime, find – O(n) runtime, O(n) space – Store input in hash table:
A simpler approach is to store each input into a hash table. 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.

/*************************************************************************
    > File Name: Datastructuredesign.cpp
    > Author: Mandagod
    > Blog: http://blog.csdn.net/mandagod
    > Mail: manda2003@163.com
    > Created Time: 2016年07月09日 星期六 00时50分22秒
 ************************************************************************/

#include<iostream>
#include<map>
using namespace std;

class Solution {
    // Implemente class
public:
    void add(int input) {
        int count = (table.find(input) != table.end()) ? table.at(input) : 0;
        table[input] = count + 1;
    }

    bool find(int val) {
        for (map<int, int>::iterator it = table.begin() ; it != table.end(); ++it) {
            int num = it->first;
            int y = val - num;
            if (y == num) {
                // For duplicates, ensure there are at least two individual numbers.
                if (it->second >= 2) return true;
            } else if (table.find(y) != table.end()) {
                return true;
            }
        }
        return false;
    }
private:
    map<int, int> table;
};

int main () {
    // add(1); add(3); add(5); find(4) -> true; find(7) -> false
    Solution sol;
    sol.add(1);
    sol.add(3);
    sol.add(5);

    string res;
    res = sol.find(4) == false ? "false" : "true";
    cout << "find(4) -> " << res << "\n";
    res = sol.find(7) == false ? "false" : "true";
    cout << "find(7) -> " << res << "\n";

    return 0;
}
/* run result
 *find(4) -> true
 *find(7) -> false
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值