LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/
题目:
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
题解:
与Two Sum类似.
设计题目考虑trade off. 如果add 很多, find很少可以采用map方法.
简历HashMap保存key 和 key的frequency. find value时把HashMap的每一个key当成num1, 来查找HashMap是否有另一个value-num1的key.
num1 = value - num1时,检查num1的frequency是否大于1.
Time Complexity: add O(1). find O(n). Space: O(n).
AC Java:
class TwoSum {
HashMap<Integer, Integer> hm;
/** Initialize your data structure here. */
public TwoSum() {
hm = new HashMap<Integer, Integer>();
}
/** Add the number to an internal data structure.. */
public void add(int number) {
hm.put(number, hm.getOrDefault(number, 0) + 1);
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
public boolean find(int value) {
for(Map.Entry<Integer, Integer> entry : hm.entrySet()){
int num1 = entry.getKey();
int num2 = value-num1;
if(hm.containsKey(num2) && (hm.get(num2)>1 || num1!=num2)){
return true;
}
}
return false;
}
}
/**
* Your TwoSum object will be instantiated and called as such:
* TwoSum obj = new TwoSum();
* obj.add(number);
* boolean param_2 = obj.find(value);
*/
考虑到trade off, 上面用HashMap时add用O(1), find用O(n).
用两个Set分别存现有nums 和现有sums可以treade off.
这种设计适合少量add, 多量find操作.
Time Complexity: add O(n). find O(1). Space: O(n).
AC Java:
public class TwoSum {
HashSet<Integer> nums;
HashSet<Integer> sums;
/** Initialize your data structure here. */
public TwoSum() {
nums = new HashSet<Integer>();
sums = new HashSet<Integer>();
}
/** Add the number to an internal data structure.. */
public void add(int number) {
Iterator<Integer> it = nums.iterator();
while(it.hasNext()){
sums.add(it.next()+number);
}
nums.add(number);
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
public boolean find(int value) {
return sums.contains(value);
}
}
/**
* Your TwoSum object will be instantiated and called as such:
* TwoSum obj = new TwoSum();
* obj.add(number);
* boolean param_2 = obj.find(value);
*/
LeetCode Two Sum III - Data structure design的更多相关文章
- [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- 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 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
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- [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 设计two sum模式 --------- java
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 笔记27 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 - ...
- [leetcode]170. Two Sum III - Data structure design两数之和III - 数据结构设计
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
随机推荐
- BZOJ3591: 最长上升子序列
因为是一个排列,所以可以用$n$位二进制数来表示$O(n\log n)$求LIS时的单调栈. 首先通过$O(n^22^n)$的预处理,求出每种LIS状态后面新加一个数之后的状态. 设$f[i][j]$ ...
- (转)Storm UI 解释
Storm UI link:http://lbxc.iteye.com/category/221265 本文主要解释下storm ui上各项属性的含义. 1. mainpage 首页主要分为3块: a ...
- Extjs 属性控件[转载]
Ext.form.TimeField: 配置项: maxValue:列表中允许的最大时间 maxText:当时间大于最大值时的错误提示信息 ...
- Mongoose简单学习笔记
1.1 名词解释 Schema : 一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力 Model : 由Schema发布生成的模型,具有抽象属性和行为的数据库操作对 Entity : 由Mo ...
- topcoder SRM 622 DIV2 FibonacciDiv2
关于斐波那契数列,由于数据量比较小, 直接打表了,代码写的比较戳 #include <iostream> #include <vector> #include <algo ...
- ACM : Travel-并查集-最小生成树 + 离线-解题报告
Travel Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u /*题意 给出n[节点 ...
- c++实现des算法
程序分三部分,des头文件,des类实现,main函数调用. //panda //2013-4-13 //des //des.h class DES { private: //public: //明文 ...
- Makefile学习
makefile中常用的函数: http://linux.chinaunix.net/techdoc/develop/2009/07/09/1122854.shtml SRC = $(wildcard ...
- Flash与JS之间相互调用以及参数传递
[AS3]ExternalInterface.call传多个参数的写法代码示例 import flash.text.TextField; ; ; var result:uint = ExternalI ...
- Hibernate映射多对多双向关联关系(小案例)
多对多双向关联关系(Project(工程)/Emp(员工)为案例): 步骤如下: 1.创建Project类,并需要定义集合类型的Emp属性 public class Project { //编号 pr ...