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
class TwoSum {
public:
void add(int number) {
++m[number];
}
bool find(int value) {
for (auto a : m) {
int t = value - a.first;
if ((t != a.first && m.count(t)) || (t == a.first && a.second > 1)) {
return true;
}
}
return false;
}
private:
unordered_map<int, int> m;
};

【leetcode】170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计的更多相关文章

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

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

  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两数之和III - 数据结构设计

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

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

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

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

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

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

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

  8. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  9. 【LeetCode】170. Two Sum III - Data structure design 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组+字典 平衡查找树+双指针 日期 题目地址:htt ...

随机推荐

  1. hdu 4521 小明序列(线段树,DP思想)

    题意: ①首先定义S为一个有序序列,S={ A1 , A2 , A3 , ... , An },n为元素个数 : ②然后定义Sub为S中取出的一个子序列,Sub={ Ai1 , Ai2 , Ai3 , ...

  2. Spring Cloud 微服务实战——nacos 服务注册中心搭建(附源码)

    作为微服务的基础功能之一的注册中心担任重要的角色.微服务将单体的服务拆分成不同的模块下的服务,而不同的模块的服务如果进行通信调用呢?这就需要服务注册与发现.本文将使用阿里开源项目 nacos 搭建服务 ...

  3. Harbor仓库搭建及使用

    目录 一.docker配置 二.安装docker-compose 三.安装harbor 四.管理harbor 五.springboot项目配置docker 六.linux服务器上打包并推送至harbo ...

  4. cesium制作自己的骑行轨迹

    制作自己的骑行轨迹 马上国庆节了,计划骑车回家,突然想到把所有的骑行线路汇总一下,无奈码表和APP不支持这样的操作,出于职业病,在此操作一下. 我用的是黑鸟码表,可以导出fit运动轨迹,但是fit还需 ...

  5. Device /dev/sdb excluded by a filter

    原因是添加的磁盘是在另一个虚拟机中新建的,已经有了分区表,现在的虚拟机并不能识别磁盘的分区表,运行parted命令重做分区表,中途需要输入三次命令(mklabel msdos -> yes-&g ...

  6. [转]技术往事:改变世界的TCP/IP协议

    原文链接 : http://www.52im.net/thread-520-1-1.html 1.前言 作为应用层开发人员,接触最多的网络协议通常都是传输层的TCP(与之同处一层的另一个重要协议是UD ...

  7. 【Java】IO流

    File类 介绍 File类的一个对象,代表一个文件或一个文件目录 File类声明在java.io包下 File类中涉及关于文件或文件目录的创建.删除.重命名.修改时间.文件大小等方法,并未涉及到写入 ...

  8. .net core api 请求实现接口幂等性

    简单实现接口幂等性,根据参数的hascode实现: 参数介绍  WaitMillisecond : 请求等待毫秒数 CacheMillisecond:请求结果缓存毫秒数 参数具体使用场景 WaitMi ...

  9. 作为Bootstrap中文站维护者-我们再次翻译BootstrapVue项目

    点击立即进入BootstrapVue中文站 http://code.z01.com/bootstrap-Vue Bootstrap-Vue 基于全球最流行的前端框架组合应用系统 项目介绍 Bootst ...

  10. zookeeper问题在公司可以 回到家用家里网不行 目前还没有解决

    Opening socket connection to server 192.168.1.193/192.168.1.193:2181. Will not attempt to authentica ...