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 首先想到用doubly linkedlist 来做, 每次加入的时候维护list 是sorted的, 然后查询的时候左右夹比就行了。 但是这样time out。
所以用hashmap来做。
public class TwoSum {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
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(Integer val : map.keySet()){
if(value == val + val && map.get(val) > 1) return true;
if(value != val + val && map.containsKey(value - val)) return true;
}
return false;
}
}
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] 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 设计two sum模式 --------- java
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
- LeetCode 笔记27 Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- leetcode3 Two Sum III – Data structure design
Question: Design and implement a TwoSum class. It should support the following operations: add and f ...
- 170. Two Sum III - Data structure design
题目: Design and implement a TwoSum class. It should support the following operations: add and find. a ...
- 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 - ...
- [Swift]LeetCode170.两数之和III - 数据结构设计 $ Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
随机推荐
- Gitlab+Jenkins学习之路(十一)之Jenkins自动触发构建和发布
思路图: 一.下载gitlab plugin jenkins-->系统管理-->管理插件-->下载并安装gitlab plugin 二.配置gitlab认证 路径:Jenkins-- ...
- 洛咕 P4528 [CTSC2008]图腾
洛咕 P4528 [CTSC2008]图腾 神题orz. 先约定abcd表示\(1\leq A<B<C<D\leq n\),而且\(y_a,y_b,y_c,y_d\)的排名正好是\( ...
- express的web server设置流程
对于express的设置,一直是拿来就用,只知其然,今天查了一下文档,记录详细过程如下. 1.实现基本常用功能需要的模块path 用来处理路径字符串拼接,设置模板路径和静态资源路径时使用cookie- ...
- C++构造函数和析构函数什么情况下会用
析构函数: 1. 对象生命周期结束,被销毁时: 2. delete 指向对象的指针时: 3. delete 指向基类对象的指针时,其析构函数是虚函数: 4. 在嵌套关系中,对象A是对象B的成员,当对象 ...
- Mysql启动失败解决方案 - 个人经验可能不适合所有场景
以前一直用的Mysql5.5,安装程序是一个exe程序,安装完了相应的服务也给我注册好了,然后直接启动连接即可. 最近升级到了8.0.15,发现和以前不一样了. 8.0.15下载地址 安装解压之后目录 ...
- Jmeter实战
Jmeter实战 入门篇 1.下载与使用 下载地址:http://jmeter.apache.org/download_jmeter.cgi 开源,基于java编写,所以得有jdk(jre)环境,下载 ...
- 最优方向法(MOD)
算法描述 求解模型: \[\min\sum\limits_i\|x_i\|_0 \quad \mathrm{s.t.} \; \|Y-DX\|^2_F \leq \varepsilon\] 或 \[\ ...
- 分布式消息队列RocketMQ与Kafka架构上的巨大差异
分布式消息服务 Kafka 是一个高吞吐.高可用的消息中间件服务,适用于构建实时数据管道.流式数据处理.第三方解耦.流量削峰去谷等场景,具有大规模.高可靠.高并发访问.可扩展且完全托管的特点,是分布式 ...
- Django缓存配置和使用
- 缓存 - 配置 CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCAT ...
- type命令详解
转自:http://codingstandards.iteye.com/blog/831504 在脚本中type可用于检查命令或函数是否存在,存在返回0,表示成功:不存在返回正值,表示不成功. $ t ...