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的更多相关文章

  1. 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 ...

  2. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  3. ✡ 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 - ...

  4. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

  5. LeetCode 笔记27 Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  6. leetcode3 Two Sum III – Data structure design

    Question: Design and implement a TwoSum class. It should support the following operations: add and f ...

  7. 170. Two Sum III - Data structure design

    题目: Design and implement a TwoSum class. It should support the following operations: add and find. a ...

  8. leetcode[170]Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  9. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  10. [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 - ...

随机推荐

  1. Openstack入门篇(十五)之如何创建生产使用的openstack镜像

    在linux-node1节点上: [root@linux-node1 ~]# yum install -y openstack-nova-compute [root@linux-node1 ~]# y ...

  2. ubuntu下su: Authentication failure的解决办法(su和su - root的区别)

    参考:ubuntu下su: Authentication failure的解决办法(su和su - root的区别)

  3. css快速入门-引入方式

    一.概述 HTML是骨架.框架CSS是外表.衣服JS是动作.肌肉 css的主要作用是对元素进行渲染.1.找到要操作的标签(选择器)2.对定位的标签进行操作(属性) 二.CSS引入方式 1.行内式 &l ...

  4. springmvc框架开发常用的注解总结

    1.@Controller使用:表示表现层中的JavaBean被spring容器管理.   2.@requestMapping使用: a) 在方法上: 标记url到请求方法的映射, 就相当于从一个ur ...

  5. 2018年美国大学生数学建模竞赛(MCM/ICM) D题解题思路

    首先整个赛题是一道集选址,优化,评价,预测的综合性赛题,对于任务 1,包括三个小问题,第一是有望完全电动化,那么就需要评价什么叫完全电动化,所以先建立一个基本的标准,比如人车比例达到多少.需要多少充电 ...

  6. 通俗理解BFS和DFS,附基本模板

    1.BFS(宽度优先搜索):使用队列来保存未被检测的节点,按照宽度优先的顺序被访问和进出队列 打个比方:(1)类似于树的按层次遍历 (2)你的眼镜掉在了地上,你趴在地上,你总是先摸离你最近的地方,如果 ...

  7. 传输控制协议--- Transmission Control Protocol (TCP)

    Transmission Control Protocol (TCP) 用于网络通信的传输控制和网络协议套件,包括很多协议,其中最主要的是TCP和IP协议.TCP/IP属于UNIX类系统的内置协议,被 ...

  8. 启动Nodejs服务

    vs code 中间创建 1.  settings.json { , { , { 'Content-Type': 'text/plain;charset=utf-8' })

  9. nodejs 几篇有用的文章

    深入浅出Node.js(三):深入Node.js的模块机制 http://www.infoq.com/cn/articles/nodejs-module-mechanism Node.js简单介绍并实 ...

  10. umask命令详解

    基础命令学习目录首页 原文链接:https://blog.csdn.net/stpeace/article/details/45509425        umask命令用得相对不多, 而umask函 ...