题目:

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

链接: http://leetcode.com/problems/two-sum-iii-data-structure-design/

题解:

设计two sum,这道题用hashmap很适合,加入是O(1),查找的话要遍历整个keyset,所以是O(n)。

Time Complexity - O(n) , Space Complexity - O(n)

public class TwoSum {
Map<Integer, Integer> map = new HashMap<>(); public void add(int number) {
if(map.containsKey(number))
map.put(number, map.get(number) + 1);
else
map.put(number, 1);
} public boolean find(int value) {
for(int i : map.keySet()) {
int res = value - i;
if(map.containsKey(res)) {
if(res == i && map.get(i) >= 2 )
return true;
if(res != i)
return true;
}
} return false;
}
}

二刷:

使用了与一刷一样的方法,这样看起来比较慢,要改进。

好好研究了一下discuss,发现了几个问题

  1. 遍历整个map的时候,用entrySet比keySet快
  2. 可以建一个set保存之前查询过并且找到答案的value
  3. 使用ArrayList来保存之前加入过的数字,再遍历这个ArrayList比遍历HashMap的keySet和entrySet都要快很多...

Java:

Time Complexity - O(n) , Space Complexity - O(n)

public class TwoSum {
Map<Integer, Integer> map = new HashMap<>();
Set<Integer> valueSet;
public TwoSum() {
map = new HashMap<>();
valueSet = new HashSet<>();
} // Add the number to an internal data structure.
public void add(int number) {
if (!map.containsKey(number)) {
map.put(number, 1);
} else {
map.put(number, 2);
}
} // Find if there exists any pair of numbers which sum is equal to the value.
public boolean find(int value) {
if (valueSet.contains(value)) {
return true;
}
for (int i : map.keySet()) {
int remain = value - i;
if (map.containsKey(remain)) {
if ((remain == i && map.get(remain) > 1) || remain != i) {
valueSet.add(value);
return true;
}
}
}
return false;
}
} // Your TwoSum object will be instantiated and called as such:
// TwoSum twoSum = new TwoSum();
// twoSum.add(number);
// twoSum.find(value);

Reference:

https://leetcode.com/discuss/76823/beats-100%25-java-code

170. Two Sum III - Data structure design的更多相关文章

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

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

  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 (两数之和之三 - 数据结构设计)$

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

  5. [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 - ...

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

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

  7. [LeetCode] 170. 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 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组+字典 平衡查找树+双指针 日期 题目地址:htt ...

随机推荐

  1. 20160611-20160714springmvc入门进阶

    springmvc第二阶段 高级知识 复习: springmvc框架: DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器 ...

  2. Android中FTP服务器搭建入门

    http://www.2cto.com/kf/201501/374048.html http://blog.csdn.net/smile3670/article/details/44343617  有 ...

  3. Operation not allowed for reason code "7" on table 原因码 "7"的解决

    对表进行任何操作都不被允许,提示SQLSTATE=57016 SQLCODE=-668 ,原因码 "7"的错误:SQL0668N Operation not allowed for ...

  4. 页面加载后的input change事件 1或2个框 ajax

    数据层没有,js和bll直接链接,数据层用的hqew. js: window.onload = function () { //型号input 改变 事件 $("#typeofproduct ...

  5. ios专题 - objc runtime 动态增加属性

    objective-c中,有类别可以在不修改源码的基础上增加方法:近排在看别人的开源代码时,发现还可以动态增加属性.而且是在运行时,太牛B了. 使用运行时库,必须要先引入 objc/runtime.h ...

  6. Eclipse查看历史代码

    选中要查看的文件(.class等) 右击->Team->Show Local History

  7. Web前端新人笔记之CSS字体

    本章内容是阅读CSS权威指南的一个小积累和随笔.新人必看,老鸟也可查看并指出不足指出以便后人阅读更好地理解.O(∩_∩)O谢谢!!!设置字体属性时样式变的最常见的用途之一:不过,尽管字体选择很重要,但 ...

  8. HTML5:基本使用

    单行文本输入框 type为text表示input元素为一个单行文本框,是input元素的默认表现形式.单行文本输入框支持下面的属性设置. A:设定元素大小 maxlength属性设定用户能够输入的字符 ...

  9. js设置cookie过期及清除浏览器对应名称的cookie

    js设置cookie过期也就相当于清除浏览器对应名称的cookie的例子. 代码: function ClearCookie() {  var expires = new Date();  expir ...

  10. oracle 11g实验五——触发器的使用

    实验要求: 实验五 触发器的使用 实验目的 1.  理解触发器的概念.作用及分类: 2.  掌握触发器的创建.使用: 实验内容 1.  建立表orders:用于存储订单列表信息:表order_item ...