[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 - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value.
Example 1:
add(1); add(3); add(5);
find(4) -> true
find(7) -> false
Example 2:
add(3); add(1); add(2);
find(3) -> true
find(6) -> false
题目
设计一个数据结构,能像其中添加数,能查找其中是否存在两数相加等于某值。
Solution1: HashMap

重头戏在find()的实现, 类似two sum的思想:遍历所有的key,同时算出remain = sum - key, 我们的任务是查找
1. key 等于 remain时, 要组成一对pair的条件是map.get(key) >1

2. key不等于remain, 要组成一对pair的条件是,remain也在map中

code
class TwoSum { // O(1) add, O(n)find
private Map<Integer, Integer> map;
/** Initialize your data structure here. */
public TwoSum() {
map = new HashMap<>();
}
/** 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, map.get(number)+1);
}
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
public boolean find(int value) {
for (Integer num : map.keySet()){
int remain = value - num;
if (( num == remain && map.get(num) > 1) || num != remain && map.containsKey(remain)){
24 return true;
}
}
return false;
}
}
注意:
我之前写成
if(remaining != n){
return map.containsKey(remaining);
}else{
if(map.get(n) > ){
return true;
}
}
一直想不通逻辑上有什么区别。
比如
add(3), add(2), add(1), find(5)
而此时遍历的key是1, 对应remaining = 4
如果按照错误的思路,程序会直接return map.containsKey(4) -> false
而程序并没有尝试key是3, 对应remaining = 2, rreturn true 的情况
---------------------------------------------------------------------------
Followup1:
要求O(1) find
思路
1. 在add()操作的时候,就用set做了pre-computation, 来记录 sum for any pair of numbers
2. 如此,在find()操作是,只需要check一下set.contains() 即可
代码
// O(n) add, O(1) find
public class TwoSumIII {
Map<Integer, Integer> map = new HashMap<>();
Set<Integer> set = new HashSet<>(); public void add(int number) {
// record sum for any pair of numbers
for (Integer n : map.keySet()){
set.add(number + n);
}
// key: each item, value: its frequency
if(!map.containsKey(number)){
map.put(number, 1);
}else{
map.put(number, map.get(number) + 1);
}
}
// set.contains() using O(1) time
public boolean find(int value) {
return set.contains(value);
}
}
[leetcode]170. Two Sum III - Data structure design两数之和III - 数据结构设计的更多相关文章
- [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] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- 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] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...
- Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...
随机推荐
- 3.2-1937 Problem D
#include <stdio.h> ; }; }; int main(void) { int n; while(scanf("%d", &n) != EOF) ...
- SSH原理及操作
1:公钥与私钥(public and private key) 公钥:提供给远程主机进行数据加密的行为 私钥:远程主机收到客户端使用公钥加密数据后,在本地端使用私钥来解密 2:公钥与私钥进行数据传输时 ...
- nginx配置http强制跳转https
nginx配置http强制跳转https 网站添加了https证书后,当http方式访问网站时就会报404错误,所以需要做http到https的强制跳转设置. 一.采用nginx的rewrite方法 ...
- sqlserver乱码问题解决
* 如果是自己创建的数据库那么就应该在一开始就选择排序规则中的:Chinese_PRC_CI_AS 1.改变排序规则方法: 右击创建的数据库,属性→选项→排序规则中选择:Chinese_PRC_CI_ ...
- 学习vue容易忽视的细节
1.对于自定义标签名(组件名称),Vue.js 不强制要求遵循 W3C 规则 (小写,并且包含一个短杠),尽管遵循这个规则比较好.HTML 特性是不区分大小写的.所以,当使用的不是字符串模板,came ...
- dede织梦后台页面及功能修改精简操作方法
有很多使用织梦程序的站长往往都不喜欢使用默认的后台,但对于很多小白站长其实也不太懂程序的功能,而且如果显示或者开了过多的功能只会给自己带来困扰,所以小白站长都喜欢一些傻瓜式的后台操作界面.那么,ded ...
- day28元类与异常查找
元类与异常处理1. 什么是异常处理 异常是错误发生的信号,一旦程序出错就会产生一个异常,如果该异常 没有被应用程序处理,那么该异常就会抛出来,程序的执行也随之终止 异常包含三个部分: ...
- suricata 配置文件threshold
threshold threshold(阈值)关键字可用于控制规则的警报频率,它有3种模式: threshold: type <threshold|limit|both>, track & ...
- exchang2010OWA主界面添加修改密码选项
原文链接:http://www.mamicode.com/info-detail-1444660.html exchange邮箱用户可以登录OWA修改密码,当AD用户密码过期或者重置密码勾选了“用户下 ...
- scp: command not found
scp 不能用? [root@doc]# scp jdk-8u144-linux-x64.tar.gz root@10.10.10.17:/root/ root@10.10.10.17's passw ...