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 - ...
随机推荐
- 【日常训练】Volleyball(CodeForces-96D)
题意与分析 这题也是傻逼题,可是我当时打比赛的时候板子出问题了- -|||,怎么调也调不过. 不过思路是很清晰的:先做n次dijkstra然后重新建图,建完了以后根据新的单向图再跑一次dijkstra ...
- 配置centos7来支持xshell远程访问和xftp传输文件
前提: 首先需要一台已装有centos7的电脑(虚拟机的配置这里不说明,这里用的是物理机) 背景: 在工作中访问linux的环境通常需要Xshell等终端软件,通过配置静态IP远程服务器进行管理开发. ...
- Unity中使用C#实现UDP广播
没有系统的学习过网络,想做联机游戏还真是费劲,想做在局域网内实现自动搜索服务器的功能,然后就想到了使用UDP进行广播,把服务器的信息广播给每一个玩家. Socket udpSocket = new S ...
- C/S结构与B/S结构
按照是否需要访问网络,程序可分为网络程序与非网络程序.其中网络程序又可分为B/S结构与C/S结构. C/S结构是指客户端(Client)/服务器(Server)模式,这种模式的客户端中 需要安装一个R ...
- [Codeforces-911B] - Two Cakes
B. Two Cakestime limit per test 1 secondmemory limit per test 256 megabytesinput standard inputoutpu ...
- 解决java读取大文件内存溢出问题
1. 传统方式:在内存中读取文件内容 读取文件行的标准方式是在内存中读取,Guava 和Apache Commons IO都提供了如下所示快速读取文件行的方法: Files.readLines(new ...
- 《Redis设计与实现》阅读笔记(三)--链表
链表 定义 链表分为两部分,链表节点和持有链表的list结构. 每个链表节点包含前置节点指针,后置节点指针,节点值void*用于保存各种不同类型的值 list结构包含表头节点指针,表尾节点指针,节点数 ...
- gitlab+jenkins持续集成--http方式连接gitlab
http://v.youku.com/v_show/id_XMjk3NTYyMDUxMg==.html?spm=a2h3j.8428770.3416059.1
- Spring AOP部分源码分析
Spring源码流程分析-AOP相关 根据Spring源码整理,其中Calculator为自定义的实现方法. AnnotationConfigApplicationContext()加载配置类的流程 ...
- MyBatis思维导图
1.初识框架技术 2.搭建MyBatis环境 3.掌握MyBatis的核心API 4.掌握MyBatis的核心配置文件:主要用于配置数据库连接和MyBatis运行时所需的各种特性 5.掌握SQL映射文 ...