170. Two Sum III - Data structure design【easy】
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】的更多相关文章
- 【LeetCode】170. Two Sum III – Data structure design
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- 【leetcode】170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations: add and find. add ...
- 【LeetCode】170. Two Sum III - Data structure design 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组+字典 平衡查找树+双指针 日期 题目地址:htt ...
- ✡ 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 - ...
- 170. Two Sum III - Data structure design
题目: Design and implement a TwoSum class. It should support the following operations: add and find. a ...
- leetcode[170]Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- [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 - ...
- [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
随机推荐
- [Codeforces 26E] MultiThreading
Brief Intro: 给你n个数,每个数有2*CNT[i]个,让你构造一个序列 使得最终的Y值为W(其余见题面) Solution: 就是一道纯构造的题目: 先把特殊情况特殊处理,接下来考虑一般情 ...
- [luogu3455]ZAP-Queries
有时候我们会遇到一类问题:求$f(n)$,当然它是不好直接计算的,但如果$F(n)=\sum\limits_{d|n}f(d)$或$F(n)=\sum\limits_{\substack{n|d\\d ...
- 【动态规划】Gym - 100923A - Por Costel and Azerah
azerah.in / azerah.out Por Costel the Pig has received a royal invitation to the palace of the Egg-E ...
- 1.5(java学习笔记)this关键字
this关键字主要有三个作用 1.调用本类中的属性. public class TextThis { public static void main(String[] args){ Person p1 ...
- KEIL3中出现的字符不对齐的情况解决办法
写代码的时候我的keil3中会出现光标不对齐的情况,如下图: 看似光标在t后面,其实是在逗号后面,这是因为字体加粗导致的.解决办法: Edit->Configuration->colors ...
- RxJava 1.x 理解-2
给RxJava 加入线程控制 -- Scheduler 在 RxJava 1.x 理解-1 中,我们说到了RxJava的简单用法,但是这还远远不够,因为这简单用法是在同一个线程中使用的.比如我们需要在 ...
- JNI概述
JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C&C++). JNI 让你在利用强大 Java 平台的同时,使你仍然可以用 ...
- 【转载】Mini6410启动过程
这段时间在尝试使用uBoot来替代友善的Superboot,让板子支持从SD卡启动,所以就仔细研究了一下友善提供的内核和它的启动参数,发现 友善真的蛮聪明,把电脑的启动方式借鉴到它们自己的开发板上了. ...
- IntelliJ IDEA字符串常量长度太长的问题解决:constant string too long
Java compiler下的Use compiler为Eclipse:
- Windows 2008 R2
1简介 Windows 2008 R2是微软针对windows 7所推出的Windows server操作系统.为了使企业减少操作成本和提高效率,Windows Server 2008 R2对企业资源 ...