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. Hibernate的Configuration对象的configure()方法

    Configuration configuration=new Configuration(); configuration.configure(); 在Hibernate底层实现configure( ...

  2. birt-j脚本调试 & 动态sql的实现

    一个比较好的birt问题解决网址: http://www.myexception.cn/h/1335919.html 1,Birt的JavaScript脚本简单调试 Birt中的js脚本不能用aler ...

  3. 鸟哥Linux学习笔记06

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

  4. 实现一个简单的虚拟DOM

    现在的流行框架,无论React还是Vue,都采用虚拟DOM. 好处就是,当我们数据变化时,无需像Backbone那样整体重新渲染,而是局部刷新变化部分,如下组件模版: <ul class=&qu ...

  5. 编号中的数学_KEY

    题目描述: 从美国州际高速公路建筑者那里,奶牛们引进了一种路径编号系统,来给牧场之间的道 路编号.他们已经把 N(1<=N<=25)个牧场,用 1 到 N 的整数编号.现在他们需要将牧场间 ...

  6. [js高手之路] html5 canvas系列教程 - 线条样式(lineWidth,lineCap,lineJoin,setLineDash)

    上文,写完弧度与贝塞尔曲线[js高手之路] html5 canvas系列教程 - arcTo(弧度与二次,三次贝塞尔曲线以及在线工具),本文主要是关于线条的样式设置 lineWidth: 设置线条的宽 ...

  7. Spring配置中<context:annotation-config> VS <context:component-scan>

    Spring 中在使用注解(Annotation)会涉及到< context:annotation-config> 和 < context:component-scan>配置, ...

  8. 最近做的floyd的题目

    基础:    HDU1596    HDU2112     HDU1874     HDU1869     HDU2066     HDU2094    HDU2544  稍加复杂: HDU1217 ...

  9. Linux内存(手动释放cache)

    项目的扩容申请了一台机器,到手之后看一下机器的指标,看到内存使用情况是这样的. 1.查看内存 free $ free -h total used free shared buffers cached ...

  10. 第四章 MySQL高级查询(二)

    第四章 MySQL高级查询(二) 一.EXISTS子查询 在执行create 或drop语句之前,可以使用exists语句判断该数据库对像是否存在,返回值是true或false.除此之外,exists ...