170. Two Sum III - Data structure design【easy】

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

这是一道收费题目,参考了grandyang的博客,感谢他!

解法一:

 class TwoSum {
public:
void add(int number) {
++m[number];
}
bool find(int value) {
for (auto a : m) {
int t = value - a.first;
if ((t != a.first && m.count(t)) || (t == a.first && a.second > )) {
return true;
}
}
return false;
}
private:
unordered_map<int, int> m;
};

哈希表的解法,我们把每个数字和其出现的次数建立映射,然后我们遍历哈希表,对于每个值,我们先求出此值和目标值之间的差值t,然后我们需要分两种情况来看,如果当前值不等于差值t,那么只要哈希表中有差值t就返回True,或者是当差值t等于当前值时,如果此时哈希表的映射次数大于1,则表示哈希表中还有另一个和当前值相等的数字,二者相加就是目标值

解法二:

 class TwoSum {
public:
void add(int number) {
s.insert(number);
}
bool find(int value) {
for (auto a : s) {
int cnt = a == value - a ? : ;
if (s.count(value - a) > cnt) {
return true;
}
}
return false;
}
private:
unordered_multiset<int> s;
};

另一种解法不用哈希表,而是unordered_multiset来做,但是原理和上面一样

参考自:http://www.cnblogs.com/grandyang/p/5184143.html

170. Two Sum III - Data structure design【easy】的更多相关文章

  1. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  2. 【leetcode】170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计

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

  3. 【LeetCode】170. Two Sum III - Data structure design 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组+字典 平衡查找树+双指针 日期 题目地址:htt ...

  4. ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java

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

  5. 170. Two Sum III - Data structure design

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

  6. leetcode[170]Two Sum III - Data structure design

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

  7. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

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

  8. [leetcode]170. Two Sum III - Data structure design两数之和III - 数据结构设计

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

  9. [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计

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

随机推荐

  1. 【树链剖分】【树状数组】【最近公共祖先】【块状树】bzoj3631 [JLOI2014]松鼠的新家

    裸题,树状数组区间修改+单点查询.当然要稍微讨论一下链的左右端点是否修改的情况咯. #include<cstdio> #include<algorithm> #include& ...

  2. Problem K: 数字菱形

    #include<stdio.h> int main() { int n,i,j,k,t,x,q,p; while(scanf("%d",&n)!=EOF) ; ...

  3. Spark1.4安装问题

    1)按照<大数据Spark企业级实战>第2章中的方法构建Spark集群,最后发现master可以正常启动,但是worker却都没有启动,原因是不能直接使用在slave模版文件 slaves ...

  4. iOS消息传递机制

    每个应用或多或少都由一些需要相互传递消息的对象结合起来以完成任务.在这篇文章里,我们将介绍所有可用的消息传递机制,并通过例子来介绍怎样在苹果的框架里使用.我们还会选择一些最佳范例来介绍什么时候该用什么 ...

  5. Android闪闪发光字体效果

    原文: http://blog.csdn.net/xu_fu/article/details/24484019 import android.content.Context; import andro ...

  6. 使用ReadOnlyCollection创建只读集合

    转载:http://www.cnblogs.com/abatei/archive/2008/02/04/1064102.html 使用泛型创建只读集合 问题 您希望类中的一个集合里的信息可以被外界访问 ...

  7. mysql 初始化报错 /usr/local/mysql/bin/mysqld:error while loading shared libraries :libaio.so.1

    安装mysql在初始化的时候,出现/usr/local/mysql/bin/mysqld:error while loading shared libraries:libaio.so.1 :canno ...

  8. iOS:MBProgressHUD的基本使用

    下载地址:https://github.com/jdg/MBProgressHUD/ //方式1.直接在View上show HUD = [[MBProgressHUD showHUDAddedTo:s ...

  9. ubuntu下wine操作usb串口

    呃,换成ubuntu后想玩下文曲星,貌似没有linux下的下载工具,只好用wine. 用的是ch340的usb转串口,不得不说ubuntu果然集大成,这些驱动都有了,用minicom设置设备为/dev ...

  10. webpacke install vue application 报错 Failed at the phantomjs-prebuilt@2.1.14 install script

    刚刚在网上下了个开源的项目: https://github.com/ing670/webappkiller 执行npm install 报错:npm ERR! Failed at the phanto ...