原题链接在这里: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的更多相关文章

  1. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  2. 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 ...

  3. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  4. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  5. [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  6. ✡ 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 - ...

  7. LeetCode 笔记27 Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  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两数之和III - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

随机推荐

  1. C++可能出错的小细节

    1. for(list<Geometry_line>::iterator it = G.begin(); it != G.end();) { if(IsLineCrossed(*it, l ...

  2. JS 特殊字符的魅力

    特殊字符的魅力 说在前面—鸭子类型 鸭子类型是动态类型的一种风格,在这种风格中,一个对象有效的语义,不是由继承自特定的类或者实现特定的接口,而是由当前方法和属性的集合决定. “当看到一只鸟走起来像鸭子 ...

  3. ACM: FZU 2148 Moon Game - 海伦公式

     FZU 2148  Moon Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  4. [WP8.1UI控件编程]Windows Phone理解和运用ItemTemplate、ContentTemplate和DataTemplate

    2.2.5 ItemTemplate.ContentTemplate和DataTemplate 在理解ItemTemplate.ContentTemplate和DataTemplate的关系的之前,我 ...

  5. 【Oracle】如何导库

    正常倒库: 步骤一:在需要导入的库里建立一个新的数据库用户 create user sms533 identified by sms533; grant dba,create session to s ...

  6. js动态创建的元素绑定事件

    新创建的元素用传统的办法无法绑定,需要用live方法. 例: $('.rule').live('mouseover', function () { $(this).addClass("can ...

  7. IOS面试题总结

    iOS面试题: 一:网络理论知识的理解 1:Internet物理地址和IP地址转换采用什么协议 ARP(Address Resolution Protocol)地址解析协议 2:Internet采用哪 ...

  8. IE6下div层被select控件遮住的问题解决方法

    Select在IE6下是处于最顶层的,因此想要遮住它,设置zIndex属性是不行的,就需要一个优先级更高的元素,就是iframe,当把iframe嵌套在弹出div层中后,把iframe设置为不可见,但 ...

  9. GO语言练习:值与引用

    1.代码 2.运行 package main import "fmt" func testValue(){ fmt.Println("for value") v ...

  10. MaterialCalendarView使用时遇到的问题

    一.概述 MaterialCalendarView是一个开源项目.功能强大支持多选.单选.标注等. 二.问题 1.其继承自ViewGroup,故与CalendarView半毛钱关系都没有,完全是一个新 ...