[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- ...
随机推荐
- verilog 代码分析与仿真
verilog 代码分析与仿真 注意:使用vivado 自带的仿真工具, reg和wire等信号需要赋予初始值 边沿检测 module signal_test( input wire cmos_pcl ...
- django路由系统URLS
usrls: from django.contrib import admin from django.urls import path from cmbd import views from dja ...
- 【OpenStack】network相关知识学习
network 类型 local:通信不跨主机,必须同一网段,主要做单机测试使用: flat:统计可以跨主机,但是需要在同一网段: 每个 flat network 都会独占一个物理网卡 计算节点上 b ...
- [UE4]Slider
Slider:滑动条 一.Slider.Bar Thickness:滑动条厚度 二.Slider.Appearance.Step Size:每次滑动的步进值 三.Slider.Appearance.V ...
- vue-cli脚手架build目录中的build.js配置文件
该随笔收藏自: vue-cli脚手架build目录中的build.js配置文件 这个配置文件是命令npm run build 的入口配置文件,主要用于生产环境 由于这是一个系统的配置文件,将涉及很多的 ...
- 虚拟机安装centOs+网络配置(完整说明)
1.新建虚拟机(标准) 选择 (我以后下安装操作系统) 选择Linux 操作系统 版本为CentOS(32位) 虚拟机的名称和位置任意 磁盘容量如下即可 设 ...
- Android SDK + Appium 环境搭建
一.JDK 安装 说明:JDK是包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具,所以必须最先安装. 链接: https://pan.baidu.com/s/1NfNK_K7vukF ...
- sqlite--一秒20万数据
参考博文:https://blog.csdn.net/weixin_35261786/article/details/78222602 #include <iostream> #inclu ...
- 第三方jar上传到Maven私服(Nexus)
mvn deploy:deploy-file -DgroupId=taobao-sdk -DartifactId=taobao-sdk-java -Dversion=1.0 -Dpackaging=j ...
- Spring中Bean及@Bean的理解
Spring中Bean及@Bean的理解 Bean在Spring和SpringMVC中无所不在,将这个概念内化很重要,下面分享一下我的想法: 一.Bean是啥 1.Java面向对象,对象有方法和属性, ...