LeetCode 170. 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.
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 (两数之和之三 - 数据结构设计)$的更多相关文章
- [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] 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 设计two sum模式 --------- java
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 - ...
- 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】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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组+字典 平衡查找树+双指针 日期 题目地址:htt ...
随机推荐
- Rigidbody(刚体) and Collider(碰撞器)
关于刚体Rigidbody,手册上是这么描述的: Control of an object's position through physics simulation. 通过物理模拟控制一个物体的位置 ...
- linux中文乱码
txt文件在linux环境下打开呈现了乱码状态. 解决方法1:在linux用iconv命令,如乱码文件名为zhongwen.txt,那么在终端输入如下命令: iconv -f gbk -t utf8 ...
- JVM菜鸟进阶高手之路二(JVM的重要性,Xmn是跟请求量有关。)
转载请注明原创出处,谢谢! 今天看群聊jvm,通常会问ygc合适吗? 阿飞总结,可能需要2个维度,1.单位时间执行次数,2.执行时间 ps -p pid -o etime 查看下进程的运行时间, 17 ...
- 我的python学习笔记一
我的python学习笔记,快速了解python,适合有C语言基础的. http://note.youdao.com/noteshare?id=93b9750a8950c6303467cf33cb1ba ...
- Ensemble Learning: Bootstrap aggregating (Bagging) & Boosting & Stacked generalization (Stacking)
Booststrap aggregating (有些地方译作:引导聚集),也就是通常为大家所熟知的bagging.在维基上被定义为一种提升机器学习算法稳定性和准确性的元算法,常用于统计分类和回归中. ...
- memcached readme
memcache======== http://www.cnblogs.com/jeffwongishandsome/archive/2011/11/06/2238265.html # 命令 ## 存 ...
- GitHub使用(二) - 新建文件夹
1.首先打开我们已经建好的仓库 "test.github.com" 页面,可以看到如下图页面,找到“新建文件Create new file”按钮并点击.
- [转]HDFS HA 部署安装
1. HDFS 2.0 基本概念 相比于 Hadoop 1.0,Hadoop 2.0 中的 HDFS 增加了两个重大特性,HA 和 Federaion.HA 即为 High Availability, ...
- poj3070矩阵快速幂
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7752 Accepted: 5501 Descrip ...
- 使用JS动态修改网页body的背景色
大部分网页默认的背景色为白色,个人感觉比较刺眼,于是写了个JS的脚本去改变body部分的背景色,代码如下: // ==UserScript== // @name ChangeBackgroundCol ...