题目:

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调优系列进阶篇 - 如何索引调优

    前言 上一篇我们分析了数据库中的统计信息的作用,我们已经了解了数据库如何通过统计信息来掌控数据库中各个表的内容分布.不清楚的童鞋可以点击参考. 作为调优系列的文章,数据库的索引肯定是不能少的了,所以本 ...

  2. 基于CAShapeLayer以及UIBezierPath的语音输入效果动画封装

    详情地址 嗯,主要是在简书上写,大家可以关注我的简书,如果有什么更好的建议也可以评论,探讨,加以斧正.

  3. asp.net 控件 导出 excel

    //导出EXCEL protected void btnDaoChu_Click(object sender, EventArgs e) { HttpContext.Current.Response. ...

  4. OC班级类

    // // MyClass.h // OC2_班级类 // // Created by zhangxueming on 15/6/12. // Copyright (c) 2015年 zhangxue ...

  5. android 权限总结

    1.拨打电话要权限 2.sd目录存东西要权限

  6. 将博客搬迁至CSDN

    CSDN不给我搬家就算了....我自己搬= = http://blog.csdn.net/ourfutr2330

  7. Java对象的序列化与反序列化:默认格式及JSON格式(使用jackson)

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3558663.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  8. shell awk

    #!/bin/bash # ;i<=;i++)); # do # c1=`shuf -i - -n ` #生成随机数 # c2=`shuf -i - -n ` # c3=`shuf -i - - ...

  9. PHP 类和继承

    //定义一个超类 //public 和 protectd属性和方法可以继承,private不可继承. class A{ public $a =0; private $b = 1; protected ...

  10. c#WebBrowser进阶

    WebBrowser的基本功能就是访问网页,但是由于它本身就不在主线程上面,所以程序判断它什么时候加载完成了,比较麻烦.为此我集合从网上找到的内容,做了一个例子. 其中包括了给WebBrowser设置 ...