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的博客,感谢他!
解法一:
1 class TwoSum { 2 public: 3 void add(int number) { 4 ++m[number]; 5 } 6 bool find(int value) { 7 for (auto a : m) { 8 int t = value - a.first; 9 if ((t != a.first && m.count(t)) || (t == a.first && a.second > 1)) { 10 return true; 11 } 12 } 13 return false; 14 } 15 private: 16 unordered_map<int, int> m; 17 };
哈希表的解法,我们把每个数字和其出现的次数建立映射,然后我们遍历哈希表,对于每个值,我们先求出此值和目标值之间的差值t,然后我们需要分两种情况来看,如果当前值不等于差值t,那么只要哈希表中有差值t就返回True,或者是当差值t等于当前值时,如果此时哈希表的映射次数大于1,则表示哈希表中还有另一个和当前值相等的数字,二者相加就是目标值
解法二:
1 class TwoSum { 2 public: 3 void add(int number) { 4 s.insert(number); 5 } 6 bool find(int value) { 7 for (auto a : s) { 8 int cnt = a == value - a ? 1 : 0; 9 if (s.count(value - a) > cnt) { 10 return true; 11 } 12 } 13 return false; 14 } 15 private: 16 unordered_multiset<int> s; 17 };
另一种解法不用哈希表,而是unordered_multiset来做,但是原理和上面一样
参考自:http://www.cnblogs.com/grandyang/p/5184143.html
转载于:https://www.cnblogs.com/abc-begin/p/7715952.html
170. Two Sum III - Data structure design【easy】相关推荐
- LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- leetcode 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object):def __init__(self):"""initialize your data str ...
- LeetCode Add and Search Word - Data structure design(字典树)
问题:设计一个支持addWord,search查询的数据结构,要求search支持.正则查询 思路:使用Trie数据结构,在匹配.时,从子结点中选取可行的一个继续匹配下一个字符,主要思路是基于递归 具 ...
- ASP.NET Core 数据保护(Data Protection)【中】
2019独角兽企业重金招聘Python工程师标准>>> API 接口 ASP.NET Core Data Protectio 主要对普通开发人员提供了两个接口,IDataProtec ...
- mysql.data.dll win10_【MySQL】Windows10下安装
2.配置环境变量 变量名:MYSQL_HOME 变量值:download_path\mysql-8.0.19-winx64 path添加:%MYSQL_HOME%\bin; 3.生成data文件 管理 ...
- Sensor信号输出YUV、RGB、RAW DATA、JPEG【转】
本文转载自:http://blog.csdn.net/southcamel/article/details/8305873 简单来说, YUV: luma (Y) + chroma (UV) 格式, ...
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题--居然还是不会么-- /*** Definition for a bi ...
最新文章
- 悟空CRM横空出世 为中小型企业献智慧
- 文巾解题 100. 相同的树
- 阿帕奇服务器文件上传,Apache HttpCore4.4基于经典的IO实现HTTP文件传输服务器
- 不能执行已释放 Script 的代码
- 分层结构的生活例子_AI的分层强化学习与人脑神经机制的联系
- Linux FTP文件服务器共享/匿名访问。别名访问。用户访问
- SQL在线练习 工具
- 伽罗华域(有限域)及其运算规则(包含大量例子)
- Ubuntu18 安装MeshLab软件
- Django项目实战——10—(修改地址前后端逻辑、删除地址前后端逻辑、设置默认地址、修改密码、虚拟机安装docker/FastDFS、电商-商品知识、首页广告数据库表分析、商品信息数据库表分析)
- 深度点击率预估模型的One-Epoch过拟合现象剖析
- win10提高开机速度只需要5步
- Discuz论坛密码与密保加密规则
- 什么叫集群?如何实现集群?
- 虚拟机构建局域网用户和组远程
- PostgreSQL 的安装以及在安装过程中遇到的问题及解决方法
- [乐意黎原创] 删除QQ的MiniBrowser浏览器,QQ聊天会话中点击链接直接用默认浏览器中打开
- C/S与B/S架构对比
- FL Studio21中文完整版升级下载
- C++实现简单UDP通信