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. 用Navicat for MySQL 连接 CentOS 6.5

     navicat for mysql windows 端 连接mysql服务器 用SSH通道访问 , 新建连接时配置如下 (两张图解释) 1.配置SSH . 配置常规

  2. angular记录

    1. <h1>{{title}}</h1> 双花括号语法是 Angular 的插值绑定语法. 这个插值绑定的意思是把组件的 title 属性的值绑定到 HTML 中的 h1 标 ...

  3. Python Flask学习

    开了一个新坑..一直以来对web的前端后端了解比较模糊,所以打算学一个后端框架,写个小博客什么的增长一下姿势水平. 初学嘛,选个相对轻量级一点的,就决定学习flask啦.

  4. MongoDB之增删改查

    MongoDB的默认端口为:27017 show  dbs   查看所有的数据库 MySQL和MongoDB的对应关系 MySQL MongoDB DB DB 数据库 table Collection ...

  5. Linux之须知

    1.服务器有哪些硬件? 主板,cpu,显卡,内存,硬盘,声卡,网卡 2.内存,CPU,硬盘的作用? cpu是管理与运算.内存:cpu和磁盘之间的缓冲设备,服务器关闭后,数据从内存中释放掉. CPU,电 ...

  6. unity中Camera.ScreenToWorldPoint

    Camera.ScreenToWorldPointVector3 ScreenToWorldPoint(Vector3 position); 将屏幕坐标转换为世界坐标. 如何转换?假如给定一个所谓的屏 ...

  7. SQL Merge 语法 单表查询

    --项目中需要用到Merg语法,于是去网上查了资料,发现竟然都是多表查询,问题是我只有一张表,于是我纳闷了,后来我灵机一动,就搞定了!--表名:t_login(登录表)--字段:f_userName( ...

  8. XSS 攻击的防御

    xss攻击预防,网上有很多介绍,发现很多都是只能预防GET方式请求的xss攻击,并不能预防POST方式的xss攻击.主要是由于POST方式的参数只能用流的方式读取,且只能读取一次,经过多次尝试,自己总 ...

  9. grep与正则表达式的使用

    正则表达式以及grep的使用 grep是一种文本过滤工具(模式:pattern)基本使用用法如下: grep [option] PATTERN FILE grep [OPTIONS] [-e PATT ...

  10. vs2017连接mysql以及问题汇总

    https://www.cnblogs.com/eye-like/p/8494355.html https://blog.csdn.net/u012658972/article/details/791 ...