Two Sum III - Data structure design LT170
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的更多相关文章
- 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] 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 Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
- LeetCode 笔记27 Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- leetcode3 Two Sum III – Data structure design
Question: Design and implement a TwoSum class. It should support the following operations: add and f ...
- 170. Two Sum III - Data structure design
题目: Design and implement a TwoSum class. It should support the following operations: add and find. a ...
- 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 - ...
随机推荐
- Using Fetch
[Using Fetch] This kind of functionality was previously achieved using XMLHttpRequest. Fetch provide ...
- Oracle11g RAC安装
双节点RAC环境,数据库 racdb 实例1:racdb1 实例2:racdb2 1.IP规划 名称 oracle-db1 oracle-db2PUBLIC I ...
- mysql 连表查询
现有tablea: tableb: ...
- 命令行执行jenkins,构建job(可传递参数)
背景| 组内做UI测试,需要每天晚上执行一遍jenkins任务,jenkins任务本身是参数化构建的.但是因为jenkins本身的定时执行没有办法指定特殊的参数,所以考虑使用命令行方式启动jenkin ...
- python3 获得shell的输出内容(subprocess.getstatusoutput)
默认通过os.system(“shell")命令赋值,结果是0之类的,0表示shell命令运行正确 如果想获得shell输出的内容,可以通过[subprocess.getstatusoutp ...
- PropertyGrid控件动态生成属性及下拉菜单 (转)
http://blog.sina.com.cn/s/blog_6f14b7010101b91b.html https://msdn.microsoft.com/zh-cn/library/ms1718 ...
- 第二章 向量(b)可扩充向量
- stm32架构初认识
刚接触stm32f373c8t6的芯片,这到底是怎末开发的,应该说它是SOC,内部有一个核心芯片,然后在芯片的外部添加了一些有特殊功能的外设,使开发者能够完成想要的功能,以stm32f373c 8t6 ...
- python函数的万能参数
我们通过一个简单的事例来展示一下函数的万能参数,我们先写一个最简单的函数 def test(*args,**kwargs): print(args,kwargs) 然后定义两个变量 l = [1,2, ...
- Windows Server RRAS 配置
在Windows Server上,RRAS 是 Rounting and Remote Access Service 的简称. 通过 RRAS UI 管理器可实现 VPN 和 NAT 的配置. RRA ...