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

Idea 1. Similar to Two Sum LT1, if numbers are unique, set would be enough, otherwise map to store frequency for each number. 遍历hashmap, 对于每个数num,找target - num,

record[num] >= 2 if target - num = num

record[target - num] >= 1 if target - num != num

Time complexity: O(1) for add, O(n) for find, or alternatively add set to store the sum, so that O(n) for add, O(1) for find

Space complexity: O(n)

 public class TwoSum {
private Map<Integer, Integer> record = new HashMap<>(); public void add(int number) {
record.put(number, record.getOrDefault(number, 0) + 1);
} public boolean find(int value) {
for(int key: record.keySet()) {
int another = value - key;
if(key == another) {
if(record.get(another) >= 2) {
return true;
}
}
else {
if(record.containsKey(another)){
return true;
}
}
}
return false;
} public static void main(String[] args) {
TwoSum subject = new TwoSum();
subject.add(1);
subject.add(3);
subject.add(4);
System.out.println(subject.find(4));
System.out.println(subject.find(7));
}
}

slightly more conciser:

 public class TwoSum {
private Map<Integer, Integer> record = new HashMap<>(); public void add(int number) {
record.put(number, record.getOrDefault(number, 0) + 1);
} public boolean find(int value) {
for(int key: record.keySet()) {
int another = value - key;
if((key == another && record.get(another) >= 2)
|| (key != another && record.containsKey(another))){
return true;
}
}
return false;
}
}

Two Sum III - Data structure design LT170的更多相关文章

  1. 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 ...

  2. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  3. ✡ 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 - ...

  4. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

  5. LeetCode 笔记27 Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  6. leetcode3 Two Sum III – Data structure design

    Question: Design and implement a TwoSum class. It should support the following operations: add and f ...

  7. 170. Two Sum III - Data structure design

    题目: Design and implement a TwoSum class. It should support the following operations: add and find. a ...

  8. leetcode[170]Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  9. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

随机推荐

  1. Java(8)中List的遍历方式

    ============Java8之前的方式==========Map<String, Integer> items = new HashMap<>();items.put(& ...

  2. 深入理解 mysql 索引

    1.资源准备 FQ软件下载:蓝灯 2.红黑树模拟:https://www.cs.usfca.edu/~galles/visualization/RedBlack.html 3.B树模拟:https:/ ...

  3. PUDN用户名与密码

    Pudn 用户名与密码 boumang8171    que2538  温馨提示:1.  95%的用户第一次登录不成功,都是因为在复制粘贴帐号和密码时,把空格也复制粘贴上了.2. 如果连续3次帐号或密 ...

  4. 三:python 对象类型详解一:数字(上)

    一:python 的数字类型: a)整数和浮点数 b)复数 c)固定精度的十进制数 d)有理分数 e)集合 f)布尔类型 g)无穷的整数精度 h)各种数字内置函数和模块 二:各种数字类型的详解 1,数 ...

  5. 批量执行命令(SSH)

    for a in {1..6} ; do scp /etc/hosts enc-bigdata0$a:/etc/hosts ; done

  6. 第二章 向量(d3)有序向量:Fibonacci查找

  7. hdu 5154 拓扑排序

    例题:hdu 5154 链接  http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思是第一行先给出n和m表示有n件事,m个关系,接下来输入m行,每行有 ...

  8. java日期加减年月日

    /** * 日期相加减 * @param time * 时间字符串 yyyy-MM-dd HH:mm:ss * @param num * 加的数,-num就是减去 * @return * 减去相应的数 ...

  9. git 基本操作命令

    1. git status 查看git 状态 2.git init 3.git push -u origin master 提交 4.git remote set "邮箱地址i" ...

  10. Gradle 实战(1)—— 配置环境变量

    背景:Gradle 是一款构建工具,继 Ant .Maven 之后的现代构建工具,我会在接下来的博文中陆续介绍,我在工作中是如何使用 Gradle 的. 下载 Gradle 下面是 Gradle 的官 ...