【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 ...
随机推荐
- 【ARC079F】Namori Grundy
Description 题目链接 大意:给一张基环外向树.要求给每一个点确定一个值,其值为所有后继点的\(\text{mex}\).求是否存在确定权值方案. Solution 首先,对于叶子节点,其权 ...
- 01-go语言开始-HelloWorld
以输出HelloWorld为目标 Go的发展史 Go语言诞生(2007年的谷歌)的背景是由于软件开发的新挑战: 多核硬件架构 超大规模分布式计算集群 Web模式导致的前所未有的开发规模和更新速度 Go ...
- (转)JDK工具-javadoc命令
背景:最近在学习java基础知识,看到文档注释部分,一种是在dos命令下生成api文件,另一种是在eclipse下生成api文件.dos方式在<疯狂java讲义>中有详细的说明,eclip ...
- Kafka+Zookeeper+Filebeat+ELK 搭建日志收集系统
ELK ELK目前主流的一种日志系统,过多的就不多介绍了 Filebeat收集日志,将收集的日志输出到kafka,避免网络问题丢失信息 kafka接收到日志消息后直接消费到Logstash Logst ...
- vue+webpack开发(三)
上一篇博文讲了怎么使用路由,这次主要讲讲怎么编写一个vue组件 vue定义了一种“单文件组件”后缀为‘.vue’的文件,大概长这样子: <template> <div> < ...
- python---基础知识回顾(三)(面向对象)
一.多继承(寻找方法) 主要学习多继承中的寻找方法的方式:分别是深度优先和广度优先 1.当类是经典类时,多继承情况下,会按照深度优先方式查找 2.当类是新式类时,多继承情况下,会按照广度优先方式查找 ...
- Java Message Service学习(一)
一,背景 近期需要用到ActiveMQ接收Oozie执行作业之后的返回结果.Oozie作为消息的生产者,将消息发送给ActiveMQ,然后Client可以异步去ActiveMQ取消息. ActiveM ...
- 安装mongodb以及设置为windows服务 详细步骤
我的win7 32的,注意版本要正确! 一.下载mongodb压缩包:mongodb-win32-i386-2.6.9.zip() 二.在D盘新建文件夹mongodb,将压缩我的解压文件放进去(有一个 ...
- python scrapy cookies 处理
def start_requests(self): cookies = 'anonymid=jcokuqwe................省略' # 首先是对cookies进行分割以;为节点 ook ...
- js调试系列: 调试基础与技巧
js调试系列目录: - 昨天我们见识到了断点的强悍,在断点的配合下进行动态调试,让读代码变的轻松不少,特别是ajax之类的.在昨天的课后练习中,确实增加了不少难度,因为 提交评论 按钮是用 jQuer ...