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 - ...
随机推荐
- Java多态的向上转型和向下转型
Java多态的向上转型和向下转型 向上转型:(子类转为父类,自动转型) 格式 :父类类型 变量名 = new 子类类型(); 注:子类赋值给父类,那这时这个父类就能调用子类的方法 向下转型:(父类转为 ...
- fiddler常用操作之断点
fiddler常用操作断点 标签(空格分隔): fiddler断点 一.断点: 1.为什么要打断点呢? 比如一个购买的金额输入框,输入框前端做了限制100-1000,那么我们测试的时候,需要测试小于1 ...
- jvisual修改内存大小
jvisual(Java VisualVM)导入dump文件内存不足解决办法: 当通过jvusual调整-Xmx参数: c:/program files/java/jdk1.6/lib/visualv ...
- ORACLE 如何产生一个随机数
1.select dbms_random.string('x', 3) from dual ; x是类型,3是长度. /* opt可取值如下: 'u','U' : 大写字母 'l','L' ...
- php 更新array键值
$arr1 = array("loginname" => "username","psw" => "password& ...
- java面试:手写代码
二分查找法. /** * 二分查找法:给定一组有序的数组,每次都从一半中查找.直到找到要求的数据. * 主要是得找到下标的表示方法. */ public class BinaryFind { /** ...
- mysql5.7.20更改root密码
my.cnf 中在[mysqld]下面增加 skip-grant-tables 使用空密码登录数据库执行下面命令 update mysql.user set authentication_string ...
- ABAP开发需要养成的习惯—程序修改数据库表
①此外将内表数据写入数据库,推荐用Modify而不是insert,因为会有些key一样的报dump loop at it_record. * 报错 * insert ...
- java 基础之--java动态代理
1.抽象角色:声明真实对象与代理对象的共同接口: 2.代理角色:相当于中介的作用,bridge,内部包含对真实角色的reference,在执行真实操作对象时,附加其他操作,相当于对真实角色的封装: 3 ...
- 11-web网页制作APP
如何将H5和WebApp 加壳成apk.ipa 问题:已经做好的纯H5的站点 想分别加两个壳子,变成apk和ipa ,要怎么实现? 要点: 1. app只是壳子,打开app直接跳转到H5的Ur ...