原题链接在这里: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. Teamwork[HDU4494]

    Teamwork Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submi ...

  2. Word2Vec源码解析

    Reference:http://blog.csdn.net/itplus/article/details/37969519  (Word2Vec解析(部分有错)) 源码:http://pan.bai ...

  3. jquery属性过滤选择器

    http://www.jb51.net/article/46279.htm   $("div[id]").addClass("highlight"); //查找 ...

  4. jquery 动画效果插件

    从jQuery API 文档中可以知道,jQuery自定义动画的函数.animate( properties [, duration] [, easing] [, complete] )有四个参数: ...

  5. 【BZOJ】2675: Bomb

    题意: 给n个点,任选其中3个点(一个点只能取一次),求选出三个点的最大曼哈顿距离之和与最小曼哈顿距离之和(n<=10^5). 题解: 最大曼哈顿距离之和很好求,就是能包围所有点的经过三个点的矩 ...

  6. Mac 下安装PHP遇到的问题

    checking for CRYPTO_free in -lcrypto... no configure: error: libcrypto not found!http://www.openssl. ...

  7. [CareerCup] 18.7 Longest Word 最长的单词

    5.7 Given a list of words, write a program to find the longest word made of other words in the list. ...

  8. tab左右箭头切换

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 关于ASPCMS标签调用的一些总结

    菜单的应用 <ul class="nav navbar-nav"> {aspcms:navlist} {}<!--判断是否有下级目录--> <li c ...

  10. BizTalk动手实验(十六)EDI-AS2解决文案开发配置

    1 课程简介 通过本课程熟悉EDI.AS2解决文案的开发与配置,本动手实验步骤及内容采用微软官方SDK完成,学员在实验过程中结合官方教程来完成本实验 本实验基于BizTalk 2013(Windows ...