【LeetCode】170. Two Sum III – Data structure design
Difficulty:easy
More:【目录】LeetCode Java实现
Description
Design and implement a TwoSum class. It should support the following operations: add
and find.
add(input) – Add the number input to an internal data structure.
find(value) – 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
Intuition
add(input): Use HashMap to store the input as key and its count as value.
find(value): similar to Two Sum
Solution
import java.util.HashMap;
import java.util.Map; public class TwoSum { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); public void add(int input) {
if (map.containsKey(input)) {
map.put(input, map.get(input) + 1);
} else
map.put(input, 1);
} public boolean find(int sum) {
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (map.containsKey(sum - entry.getKey())) {
if (entry.getKey() == sum - entry.getKey() && entry.getValue() == 1)
return false;
return true;
}
}
return false;
} public static void main(String[] args) {
TwoSum a = new TwoSum();
a.add(2);
System.out.println(a.find(4)==false);
a.add(2);
a.add(6);
System.out.println(a.find(4)==true);
System.out.println(a.find(12)==false);
System.out.println(a.find(8)==true);
System.out.println(a.find(2)==false);
a.add(10);
System.out.println(a.find(12)==true);
}
}
true
true
true
true
true
true
TwoSum
Complexity
Time complexity :
For Method add(): O(1)
For Method find(): O(n) (not O(1), because we need to iterate through the hashMap)
Space complexity :
O(n) for storage
What I've learned
1. Be aware of the duplicates when writing the method find().
2. How to iterate through an HashMap? Refer to 遍历HashMap
More:【目录】LeetCode Java实现
【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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组+字典 平衡查找树+双指针 日期 题目地址:htt ...
- 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 (两数之和之三 - 数据结构设计)$
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 设计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 - ...
- [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 - ...
- 170. Two Sum III - Data structure design
题目: Design and implement a TwoSum class. It should support the following operations: add and find. a ...
随机推荐
- 【BZOJ1083】[SCOI2005]繁忙的都市(最小生成树)
[BZOJ1083][SCOI2005]繁忙的都市(最小生成树) 题面 BZOJ 洛谷 题解 模板题. #include<iostream> #include<cstdio> ...
- 【Luogu1344】追查坏牛奶(最小割)
[Luogu1344]追查坏牛奶(最小割) 题面 洛谷 题解 裸的最小割,但是要求边的数量最小. 怎么办呢?给每条边的权值额外加上一个很大的值就了. #include<iostream> ...
- 【转】I²C总线上拉电阻阻值如何选择?
I2C总线为何需要上拉电阻? I2C(Inter-Intergrated Circuit)总线是微电子通信控制领域中常用的一种总线标准,具有接线少,控制方式简单,通信速率高等优点. I2C总线的内部结 ...
- Intel 8086_通用寄存器|段寄存器
- Java基础-类加载机制与自定义类Java类加载器(ClassLoader)
Java基础-类加载机制与自定义类Java类加载器(ClassLoader) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 关于类加载器的概念和分类我就不再废话了,因为我在之前的笔 ...
- element-ui合并行:span-method
objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0) { if (rowIndex % 2 ...
- css3 @keyframes用法
使用@keyframes规则,可以创建动画. 在动画的过程中,可以多次更改css样式的设定. 对于指定的变化:发生时用0%,或关键字“from”和“to”,这与0%和100%相同. 0%:开头动画. ...
- CSS Counter Style试玩儿
2015年2月3日,CSS Counter Style level3成为了W3C的候选标准,是时候来一探究竟,看看强大魔力的@counter-style如何自定义list-style和counter. ...
- weblogic11G 修改密码
weblogic11的登录密码修改方法: 1. 登陆到weblogic后选中domain structure下的security Realms(如图一) (图一) 详情如图二: (图二) 2. 双 ...
- hdu 4857 Little Devil I
http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意:给你一棵树,边的颜色要么为白色,要么为黑色,初始每条边为白色,有三种操作 1.将u-v链上面的所有边 ...