[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.
Example 1:
add(1); add(3); add(5);
find(4) -> true
find(7) -> false
Example 2:
add(3); add(1); add(2);
find(3) -> true
find(6) -> false
这道题让我们设计一个 Two Sum 的数据结构,跟 LeetCode 的第一道题 Two Sum 没有什么太大的区别,作为 LeetCode 的首题,Two Sum 的名气不小啊,正所谓平生不会 TwoSum,刷尽 LeetCode 也枉然。记得原来在背单词的时候,总是记得第一个单词是 abandon,结果有些人背来背去还在 abandon,有时候想想刷题其实跟背 GRE 红宝书没啥太大的区别,都是一个熟练功夫,并不需要有多高的天赋,只要下足功夫,都能达到一个很不错的水平,套用一句鸡汤问来激励下吧,“有些时候我们的努力程度根本达不到需要拼天赋的地步”,好了,不闲扯了,来看题吧。不过这题也没啥可讲的,会做 Two Sum 的这题就很简单了,先来看用 HashMap 的解法,把每个数字和其出现的次数建立映射,然后遍历 HashMap,对于每个值,先求出此值和目标值之间的差值t,然后需要分两种情况来看,如果当前值不等于差值t,那么只要 HashMap 中有差值t就返回 True,或者是当差值t等于当前值时,如果此时 HashMap 的映射次数大于1,则表示 HashMap 中还有另一个和当前值相等的数字,二者相加就是目标值,参见代码如下:
解法一:
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;
};
另一种解法不用 HashMap,而是 unordered_multiset 来做,但是原理和上面一样,参见代码如下:
解法二:
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;
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/170
类似题目:
参考资料:
https://leetcode.com/problems/two-sum-iii-data-structure-design/
https://leetcode.com/problems/two-sum-iii-data-structure-design/discuss/52015/Beats-100-Java-Code
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计的更多相关文章
- [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 - ...
- LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- LeetCode 371. Sum of Two Integers (两数之和)
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- ✡ 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 - ...
- LeetCode 笔记27 Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
随机推荐
- Node基础篇(模块和NPM)
核心模块 核心模块的意义 如果只是在服务器运行JavaScript代码,意义并不大,因为无法实现任何功能(读写文件,访问网络). Node 的用处在于它本身还提供的一系列功能模块,用于与操作系统互动. ...
- LINQ to SQL语句(10)之Insert
1.简单形式 说明:new一个对象,使用InsertOnSubmit方法将其加入到对应的集合中,使用SubmitChanges()提交到数据库. var newCustomer = new Custo ...
- PHP中模拟JSONArray
前面整理过一篇文章,描述php中的array与json的array和object的转换关系.http://www.cnblogs.com/x3d/p/php-json-array-object-typ ...
- 工作中常用的js、jquery自定义扩展函数代码片段
仅记录一些我工作中常用的自定义js函数. 1.获取URL请求参数 //根据URL获取Id function GetQueryString(name) { var reg = new RegExp(&q ...
- Mybatis配置一对多的关联关系(五)
问题:是查询一个部门中的员工? 一.web项目构架 二.lib文件的jar 三.配置大小配置和该工具类 1大配置mybatis-config.xml <?xml version="1. ...
- iOS:frame访问、设置简化
看到一些程序都有这种写法,也不知道原创者是谁了.先在博客保存下. 在.m文件 #import "UIView+MyFrameCategory.h" @implementation ...
- iOS中NSLog输出格式大全
iOS开发中的输出格式大全: %@ 对象 %d, %i 整数%u 无符整形%f ...
- iOS 学习 - 3.仿qq列表
完整代码放在 github 上面 https://github.com/slodier/SimilarQQ
- 用SVN check out项目后第三方库丢失
曾经用Cornerstone check out 一份项目下来,但其中第三方.a库始终丢失,项目报错,研究后找到了以下解决方法: 首先,Xcode默认忽略.a 文件.所以无法提交到svn服务器,但是很 ...
- 树莓派:raspberry pi 3b - NOOBS
NOOBS - 多操作系统安装器,可以将不同支持树莓派的流行操作系统安装在一张SD卡中并提供一个启动管理工具,安装的不同操作系统相互独立,互不影响,是一种比较有意思的玩法. 从版本1.3.1开始到1. ...