题目:

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. SQL SERVER中的逻辑读,预读和物理读

    sqlserver:数据存储方式:最小单位是页,每一页8k,sqlserver 对页的读取是具有原子性,也就是说,要么读取完整一页,要么完全不读取,不会有中间状态,而页之间的数据组织结构是B树 但是每 ...

  2. NDIS小鱼防火墙之拦截指定QQ登录

    因为QQ窗口是自绘窗口,gif录制软件不能正常录制到窗口,我就截图功能把窗口显示出来. 下面我的演示,我登录2个QQ号,一个被我指定拦截,让他不能够登录.另一个却可以正常的工作..看我的COOL的演示 ...

  3. Centos7最小化安装后(minimal)安装图形界面

    centos7下载地址:http://mirrors.cqu.edu.cn/CentOS/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso 下载后用vmwa ...

  4. ASP.NET MVC模型绑定的6个建议(转载)

    ASP.NET MVC模型绑定的6个建议 发表于2011-08-03 10:25| 来源博客园| 31 条评论| 作者冠军 validationasp.netmvc.netasp 摘要:ASP.NET ...

  5. 实现ios屏幕的横竖屏自适应

    整理总结中... 刷新 可以通过 -setNeedsUpdateConstraints -layoutIfNeeded 两个方法来刷新约束的改变,使UIView重新布局, 和CoreGraphic的- ...

  6. Bootstrap使用心得

    久闻Twitter的Bootstrap框架强大且易用,近日为改版小丸工具箱的官网特地花了一周实践. 这篇文章总结我在使用Bootstarp中的一些关键点. 1.布局 Bootstrap框架的布局采用了 ...

  7. java集合 collection-list-LinkedList 模拟一个堆栈或者队列数据结构。

    /* 使用LinkedList模拟一个堆栈或者队列数据结构. 堆栈:先进后出 如同一个杯子. 队列:先进先出 First in First out FIFO 如同一个水管. */ import jav ...

  8. jquery改变多个css样式

    <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax ...

  9. php生成随机产生六位数密码的代码

    php生成随机产生六位数密码的代码,供大家学习参考.本文转自:http://www.jbxue.com/article/6199.html php生成随机产生六位数密码的代码,供大家学习参考. 复制代 ...

  10. Angular 动态生成html中 ng-click无效

    bodyApp.controller('customersCtrl', function ($scope, $http, cfpLoadingBar,$compile) { $scope.test = ...