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

题目标签:Hash Table

  题目让我们设计一个 两数之和的 class, 要有add(number) 和 find(value)功能。

  建立HashMap,number 当作 key;number 出现的次数当作 value保存。

  要注意的是,两数之和的 两数可以是相同的数字,出现两次。比如 2 + 2 = 4 也是可以的。

Java Solution:

Runtime beats 82.39%

完成日期:05/16/2017

关键词:HashMap

关键点:HashMap<number, occurrence>

 class TwoSum
{
HashMap<Integer, Integer> map;
/** Initialize your data structure here. */
public TwoSum()
{
map = new HashMap<>();
} /** Add the number to an internal data structure.. */
public void add(int number)
{
map.put(number, map.getOrDefault(number, 0) + 1);
} /** Find if there exists any pair of numbers which sum is equal to the value. */
public boolean find(int value)
{
for(int num: map.keySet())
{
int target = value - num; if(num == target)
{
if(map.get(num) > 1)
return true;
}
else
{
if(map.containsKey(target))
return true;
}
} return false;
}
} /**
* Your TwoSum object will be instantiated and called as such:
* TwoSum obj = new TwoSum();
* obj.add(number);
* boolean param_2 = obj.find(value);
*/

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$的更多相关文章

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

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

  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] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

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

  4. [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 - ...

  5. ✡ 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 - ...

  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. 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 ...

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

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

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

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

随机推荐

  1. Mysql的基本命令图

    如果看不清的,右键图片在新标签页打开! 这是经过我的整理出来的!如果有重要的再补充把-..

  2. 鸟哥Linux学习笔记06

    Linux 系统常用的压缩命令 1,*.Z compress程序压缩的文件,这个已经很老了,几乎不再使用,因此不再介绍. 2,gzip应用最广泛的压缩命令.目前gzip可以解开compress.zip ...

  3. GitHub使用(二) - 新建文件夹

    1.首先打开我们已经建好的仓库 "test.github.com" 页面,可以看到如下图页面,找到“新建文件Create new file”按钮并点击.

  4. 使用千位分隔符(逗号)表示web网页中的大数字

    做手机端页面我们常常遇到数字,而在Safari浏览器下这些数字会默认显示电话号码,于是我们就用到了补坑的方法加入<meta>标签: <meta name="format-d ...

  5. Hive如何添加第三方JAR

    以加入elsaticsearch-hadoop-2.1.2.jar为例,讲述在Hive中加入第三方jar的几种方式. 1,在hive shell中加入 [hadoop@hadoopcluster78  ...

  6. numpy学习整理

    今天先整理到这里,剩下的下次再整理 1.改变形状: reshape()返回改变的数组形状,但无法改变源数组形状 resize() 可以改变源数组形状 ravel() 输出类似C数组的列表,和resha ...

  7. Hexo + GitHub Pages搭建博客

    搭建 Node.js 环境 为什么要搭建 Node.js 环境? – 因为 Hexo 博客系统是基于 Node.js 编写的 Node.js 是一个基于 Chrome V8 引擎的 JavaScrip ...

  8. 怎样清理c盘垃圾

    休眠文件清理 休眠文件(hiberfil.sys)是,当你的电脑进入休眠状态时,系统临关闭前会将所有内存内容写入hiberfil.sys文件.当你重新打开电脑时,系统在将hiberfil.sys文件内 ...

  9. ZOJ2286 Sum of Divisors 筛选式打表

    我想我是和Segmentation Fault有仇,我一直以为是空间开大的问题,然后一直减少空间,还是SF,谁让n没有给范围了,qwq. 教训:以后注意输入范围和开的空间大小. #include< ...

  10. 数据库中删除语句Drop、Delete、Truncate的相同点和不同点的比较

    数据库删除语句的分别介绍: Delete:用于删除表中的行(注:可以删除某一行:也可以在不删除表的情况下(即意味着表的结构.属性.索引完整)删除所有行) 语法:删除某一行:Delete From 表名 ...