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 ...
随机推荐
- cisco通过控制口或者通过远程配置交换机
学而不思则罔,思而不学则殆,每天坚持一小步,则成功一大步 下面我们通过Cisco Packet来模拟交换机和路由器的远程和控制台登录配置交换机. 交换机console口的连接与配置方法 (1),在Ci ...
- WCF编程系列(二)了解WCF
WCF编程系列(二)了解WCF 面向服务 服务是复用进化的结果,起初的复用是函数,面向对象编程的出现使复用从函数上升到对象,随后面向组件编程又将复用从对象上升到组件,现在面向服务编程将复用 ...
- android 登陆案例_最终版本 sharedpreference
xml 与之前的登陆案例相同 java代码: package com.itheima.login; import java.util.Map; import com.itheima.login.ut ...
- Java实战之03Spring-01Spring概述
一.Spring概述 1.Spring是什么? Spring是分层的Java SE/EE应用 full-stack轻量级开源框架,以IoC(Inverse Of Control:反转控制)和AOP(A ...
- C语言中的%0nd,%nd,%-nd
C语言中的%0nd printf --> formatted print/格式化输出 一.十进制 d -> decimal/十(shí)进制 int a=1; int b=1234; do ...
- sgu 108 Self-numbers II
这道题难在 hash 上, 求出答案很简单, 关键是我们如何标记, 由于 某个数变换后最多比原数多63 所以我们只需开一个63的bool数组就可以了! 同时注意一下, 可能会有相同的询问. 我为了防止 ...
- 开发日志系列:一个表单页面的呈现与提交(一)——JSON的操作
JSON操作 引子 最近在做一个表单页面,大概是这个样子的 这里打算用一个JSON存储所有的信息,我们可以理解为,所有东西都存在一个字符串里面.方便,快捷,易读,数据库操作也方便了.甚至,可以将很多不 ...
- VS2010 EntityFramework Database First
本文演练介绍如何使用实体框架进行 Database First 开发.通过 Database First,可以从现有数据库对模型进行反向工程处理.模型存储在一个 EDMX 文件(扩展名为 .edmx) ...
- 【原创】开机出现grub rescue,修复办法
出现这种问题 一般在于进行了磁盘分区(GHOST备份时也会造成)导致grub引导文件找不到.我们只要让它找到引导文件就好了. 此时屏幕上提示grub resume> 我们先输入set看下现在g ...
- java Object类
常用的共性内容 1,实现任何对象的比较,一般比较同一种对象的比较 Object1.equals(Object obj);等同于Object1 == obj: 只有当两个引用指向同一个对象时方法返回tr ...