[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 - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value.
Example 1:
add(1); add(3); add(5);
find(4) -> true
find(7) -> false
Example 2:
add(3); add(1); add(2);
find(3) -> true
find(6) -> false
题目
设计一个数据结构,能像其中添加数,能查找其中是否存在两数相加等于某值。
Solution1: HashMap

重头戏在find()的实现, 类似two sum的思想:遍历所有的key,同时算出remain = sum - key, 我们的任务是查找
1. key 等于 remain时, 要组成一对pair的条件是map.get(key) >1

2. key不等于remain, 要组成一对pair的条件是,remain也在map中

code
class TwoSum { // O(1) add, O(n)find
private Map<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) {
if(!map.containsKey(number)){
map.put(number, 1);
}else{
map.put(number, map.get(number)+1);
}
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
public boolean find(int value) {
for (Integer num : map.keySet()){
int remain = value - num;
if (( num == remain && map.get(num) > 1) || num != remain && map.containsKey(remain)){
24 return true;
}
}
return false;
}
}
注意:
我之前写成
if(remaining != n){
return map.containsKey(remaining);
}else{
if(map.get(n) > ){
return true;
}
}
一直想不通逻辑上有什么区别。
比如
add(3), add(2), add(1), find(5)
而此时遍历的key是1, 对应remaining = 4
如果按照错误的思路,程序会直接return map.containsKey(4) -> false
而程序并没有尝试key是3, 对应remaining = 2, rreturn true 的情况
---------------------------------------------------------------------------
Followup1:
要求O(1) find
思路
1. 在add()操作的时候,就用set做了pre-computation, 来记录 sum for any pair of numbers
2. 如此,在find()操作是,只需要check一下set.contains() 即可
代码
// O(n) add, O(1) find
public class TwoSumIII {
Map<Integer, Integer> map = new HashMap<>();
Set<Integer> set = new HashSet<>(); public void add(int number) {
// record sum for any pair of numbers
for (Integer n : map.keySet()){
set.add(number + n);
}
// key: each item, value: its frequency
if(!map.containsKey(number)){
map.put(number, 1);
}else{
map.put(number, map.get(number) + 1);
}
}
// set.contains() using O(1) time
public boolean find(int value) {
return set.contains(value);
}
}
[leetcode]170. Two Sum III - Data structure design两数之和III - 数据结构设计的更多相关文章
- [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] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- 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] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...
- Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...
随机推荐
- JS 验证字符串是否为空
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 导入导出Oracle
- 回顾ThreadLocal
ThreadLocal作为解决特定场景下并发的一种方案,在Spring等框架及面试中经常会被问到,它是Java必须要掌握的基础知识之一. ThreadLocal类的作用是抽象线程内变量的抽象,这类对象 ...
- 3.2 MathType插入竖省略号
法1:鼠标单击位于上方菜单栏下面的空格和椭圆模版,然后在下拉模版下选择垂直省略号,如下图所示. 按照上面的操作选择垂直省略号模版后,这样在下面的编辑区域就会打出该符号了. 法2:在插入符号下查找 如果 ...
- socket资源
http://www.360doc.com/content/13/1231/16/14919052_341525862.shtml Linux下基于socket多线程并发通信的实现 https://w ...
- Ubuntu 14.10 下安装Ambari 问题汇总
在编译安装Ambari时候遇到了很多问题,现在记录一下 1 got error npm ERR! phantomjs@1.9.12 install while building ambari-web ...
- [UE4]添加手柄
一.在上一节的VRPawnBase中,再添加2个Motion Controller,分别命名为:LeftMotionController.RightMotionController,分别代表左右手柄. ...
- 2017-2018-2 20165312 实验三《敏捷开发与XP实践》实验报告
2017-2018-2 20165312 实验三<敏捷开发与XP实践>实验报告 一.实验内容 1.XP基础 极限编程(Extreme Programming,XP)是一种全新而快捷的软件开 ...
- select函数与I/O多路转接
select函数与I/O多路转接 相作大家都写过读写IO操作的代码,例如从socket中读取数据可以使用如下的代码: while( (n = read(socketfd, buf, BUFSIZE) ...
- SAS LOGISTIC 逻辑回归中加(EVENT='1')和不加(EVENT='1')区别
区别在于:最大似然估计分析中估计是刚好正负对调加上EVENT:%LET DVVAR = Y;%LET LOGIT_IN = S.T3;%LET LOGIT_MODEL = S.Model_Params ...