【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 ...
随机推荐
- 【转】如何解决使用keil下载或者调试程序是提示的“Invalid ROM Table”信息!
在将Discovery的工程移植到Mini-STM32F4x9BI开发板时,使用ULINK2下载程序可能会出现如下图所示的“Invalid ROM Table”这个错误. 可能原因是使用Mini-ST ...
- Python 基础数据类型之tuplu
Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 1.元组的定义 tuple1 = ("hello", "world&qu ...
- Java基础-爬虫实战之爬去校花网网站内容
Java基础-爬虫实战之爬去校花网网站内容 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 爬虫这个实现点我压根就没有把它当做重点,也没打算做网络爬虫工程师,说起爬虫我更喜欢用Pyt ...
- Linux问题集锦
一些会遇到的问题,我会不断更新问题集锦~ 1.vi / vim保存文件时遇到的问题:E212: Can't open file for writing 在vi / vim下输入w或wq!保存编辑的文 ...
- SHELL (2) —— Shell变量的核心基础知识和实践
摘自:Oldboy Linux运维——SHELL编程实战 Shell变量:用一个固定的字符串(也可能是字符.数字等的组合)代替更多.更复杂的内容,该内容里可能还会包含变量.路径.字符串等其它的内容. ...
- poj 1776 Task Sequences
http://poj.org/problem?id=1776 题意: 有一个机器要完成N个作业, 给你一个N*N的矩阵, M[i][j]=1,表示完成第i个作业后不用重启机器,继续去完成第j个作业 M ...
- Struts2_day03
一.上节回顾 1 在action获取表单提交数据 (1)使用ActionContext类获取 (2)使用ServletActionContext类获取 (3)接口注入 2 结果配置 (1)全局结果页面 ...
- js拾遗: 函数字面量
今天落叶同学发我一篇文章,我看到一个"新"名词 "函数字面量" (也可叫直接量),当时我就郁闷了,这是什么东西? 我怎么没听说过..回头翻了下权威指南,在第 4 ...
- XMPP 基础
CHENYILONG Blog XMPP 基础 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong ...
- shift 用法
shift shift命令用于对参数的移动 (左移),通常用于在不知道传入参数个数的情况下依次遍历每个参数然后进行相应处理(常见于Linux中各种程序的启动脚本). 示例 1 示例 依次读取输入的 ...