170. 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
链接: 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,发现了几个问题
- 遍历整个map的时候,用entrySet比keySet快
- 可以建一个set保存之前查询过并且找到答案的value
- 使用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的更多相关文章
- 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 ...
- ✡ 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 - ...
- leetcode[170]Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- [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 - ...
- 【LeetCode】170. Two Sum III – Data structure design
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- 【leetcode】170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations: add and find. add ...
- 【LeetCode】170. Two Sum III - Data structure design 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组+字典 平衡查找树+双指针 日期 题目地址:htt ...
随机推荐
- oracle 11G创建表空间、用户、配置监听和TNS
最近总在安装各种版本的oralce数据库做测试,11G,32位的,64位的,12C的,每次都折腾表空间,用户.tns啥的,这里记录下,再也不用现用现百度找了 一.创建表空间.用户 在plsql工具中 ...
- jquery动态插入行
这是一个利用jquery动态插入输入内容的代码. html代码: <div title="分表2" class="ui-edit" style=" ...
- 给String添加reverse方法
我们知道Array有个reverse方法,String则没有,但可以Array来实现,字符串有个split方法可以轻易的将String转换为Array. String.prototype.revers ...
- web前端面试题收集(一)
CSS中margin和padding的区别? Javascript中如何检测一个变量是一个String类型?请写出函数实现. 网页中实现一个计算当年还剩多少时间的倒计时程序,要求网页上实时动态显示“x ...
- WTL 中CComboBoxEx显示不了的问题
在使用WTL的CComboBoxEx时,InsertItem之后,运行程序,ComboBox显不了问题,其原因如下: I guess you want to place combo box to di ...
- C++里的int 和string类型相互转换
C++不像Java和C#一样在进行数据类型转换时直接调用一些类方法就可以了,使用起来很简单. 一个很简单的例子就是string str=“D:\\”+1+“.txt”;这在Java或者C#里面是可以自 ...
- OpenJudge 2756 二叉树
1.链接地址: http://bailian.openjudge.cn/practice/2756/ 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 如 上图所示,由正整数1 ...
- 支付宝api教程,支付宝根据交易号自动充值
最近公司要用php做一个网站支付宝自动充值的功能,具体就是客户把钱直接转到公司的支付宝账号里,然后在我们网站上填写上交易号,我们网站程序自动获取交易信息,自动给网站的账户充值. 我的具体想法就是利用支 ...
- fuser 命令概述
fuser 概述 fuser命令是用来显示所有正在使用着指定的file, file system 或者 sockets的进程信息. 例一: #fuser –m –u /mnt/usb1 /mnt/us ...
- 一个基本jquery的评论留言模块
<div class="productDiscuss"> <div class="title"><span class=" ...