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 - ...
随机推荐
- springboot org.hibernate.MappingException: No Dialect mapping for JDBC type: -9
参考 https://www.cnblogs.com/luxd/p/8316243.html https://www.cnblogs.com/s648667069/p/6478559.html @Co ...
- CentOS 下部署weblogic12.1.1.0
1.软件包 JDK 1.7.0 Weblogic 12.1.1.0 2.JDK安装完配置环境变量 export JAVA_HOME=/usr/java/jdk1.7.0_79 export CLASS ...
- nodejs中.npmrc文件的内容
. nodejs安装后,使用npm安装模块的时候我出现了一个错误. getaddrinfo ENOTFOUND xxx 主要是这个配置文件的问题.搞不清楚.直接打开把文件内容删除变成 npmrc文件内 ...
- WdatePicker控件中日期的范围选择
1.开始日期不能大于结束日期,结束日期只能选择今天之前(不包括今天) <asp:TextBox ID="T_CREATION_DATE_Start" Width=" ...
- Leading and Trailing (数论)
Leading and Trailing https://vjudge.net/contest/288520#problem/E You are given two integers: n and k ...
- 公告栏添加时钟——利用canvas画出一个时钟
前言 最近在学习HTML5标签,学到Canvas,觉得很有趣.便在慕课网找了个demo练手.就是Canvas时钟. 对于canvas,w3shcool上是这么描述的: HTML5 <canvas ...
- 解决node-pre-gyp install --fallback-to-build 卡住不动
一般是因为需要下载国外的包,要么连VPN,要么使用淘宝的镜像: 使用cnpm: npm install -g cnpm --registry=https://registry.npm.taobao.o ...
- 【php 之根据函数名称动态调用该函数】
解释函数:call_user_func()以及函数call_user_func_array() 对于PHP程序员而言,函数是再熟悉不过的事物了,毕竟我们整天都在和PHP内置函数以及我们自定义的函数打交 ...
- 8.17 纯css画一个着重号图标
今天看到一个同事写的着重号图标,我以为是图片,仔细一看,是span标签!哇!!学习一下哈哈 图标长这样: CSS代码: .hint{ display: inline-block; width: 20p ...
- 【linux】下Apache无法启动(8080端口被占用)
Linux下8080端口被占用,apache无法启动. 打开终端输入netstat -lnp|grep 8080 发现竟然是tcp6 占用里,因此ipv6启用占用了端口. 1.打开/etc/sysct ...