原题链接在这里: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. IO中同步、异步与阻塞、非阻塞的区别

    一.同步与异步同步/异步, 它们是消息的通知机制 1. 概念解释A. 同步所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回. 按照这个定义,其实绝大多数函数都是同步调用(例如si ...

  2. 移动端HTML5开发心得(转)

    1. iOS里fixed中有input或者textarea,用户在里面输入文字,触发键盘,fixed容器会居中显示,而不是继续悬浮       解决办法: http://dwz.cn/CrwNz 2. ...

  3. AFNetworking 2.0 出现Use of undeclared identifier AFURLSessionManager错误

    当向下面使用时会出现错误 #import "AFNetworking.h" #import "AFURLSessionManager.h" AFURLSessi ...

  4. 状压dp题目总结

    这块比较薄弱.. 来几道水题: BZOJ1231: [Usaco2008 Nov]mixup2 混乱的奶牛 f[i][j]状态i结尾j的个数 ;i<=tot;i++) ;j<=n;j++) ...

  5. Ninject使用demo

    public class HomeController : Controller { public ActionResult Index() { //核心对象 IKernel ninjectKerne ...

  6. BZOJ4532: [BeiJing2014 WinterCamp] 珠链

    Description Alex喜欢玩网络游戏,认为这是智力和体力的综合锻炼.在一次游戏活动中,他意外获得了一个传说中威力极其强大的法宝:珠链.  珠链,顾名思义,就是由许多小珠子串起来的一条链.珠子 ...

  7. 硬盘分区工具gparted使用

    一.介绍 GParted是一款linux下的功能非常强大的分区工具,和windows下的‘分区魔术师’类似,操作和显示上也很相似.GParted可以方便的创建.删除分区,也可以调整分区的大小和移动分区 ...

  8. 李洪强iOS面试题之-iOS选择题

    1.及时聊天app不会采用的网络传输方式是 DA UDP B TCP C Http D FTP 2.下列技术不属于多线程的是 AA Block B NSThread C NSOperation D G ...

  9. 记录JVM内存模型,参数含义和优化

    一.JVM内存模型 (图片来自网络) 根据Java虚拟机规范,JVM将内存划分为: New(年轻代) Tenured(年老代) Perm (永久代) 其中New和Tenured属于堆内存,堆内存会从J ...

  10. JAVA中JDBC连接数据库

    这里列举了JDBC连接Oracle . SQLServer .MySQL 三种 数据库 1.Oracle连接(导入classes12.jar 包) public static Connection g ...