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. Centos7 升级过内核 boot分区无法挂载修

    参考连接:https://www.cnblogs.com/heqiuyong/p/11186301.html 故障图 挂载系统盘,光盘启动,急救模式, chroot /mnt/sysimage 报错 ...

  2. adb 安装与使用(一)

    一.ADB简介 1. 什么是adb? adb(Android Debug Bridage)是Android sdk的一个工具: adb 是用来连接安卓手机和PC端的桥梁,要有adb作为二者之间的维系, ...

  3. git删除未被追踪的文件

    # 先看看会删掉哪些文件,防止重要文件被误删 git clean -n # 删除 untracked files git clean -f # 连 untracked 的目录也一起删掉 git cle ...

  4. Unicode、UTF-8、UTF-16 终于懂了

    计算机起源于美国,上个世纪,他们对英语字符与二进制位之间的关系做了统一规定,并制定了一套字符编码规则,这套编码规则被称为ASCII编码 ASCII 编码一共定义了128个字符的编码规则,用七位二进制表 ...

  5. pylint代码静态检查

    使用git-pylint-commit-hook工具 pre-commit脚本 配置文件放在仓库根目录/hooks下面 git-pylint-commit-hook --limit=9.0 --pyl ...

  6. celery kill task

    from celery.task.control import revokerevoke(task_id, terminate=True) https://stackoverflow.com/ques ...

  7. 详解calc()函数功能

    calc()对大家来说,或许很陌生,不太会相信calc()是css中的部分.因为看其外表像个函数,既然是函数为何又出现在CSS中呢?这一点也让我百思不得其解,今天有一同事告诉我,说CSS3中有一个属性 ...

  8. url,href,src 之间的区别

    url 统一资源定位符 <style> #bg{ background-image:url("img/bg.png"); } </style> 区别: sr ...

  9. Django 小实例S1 简易学生选课管理系统 8 CSS样式优化

    Django 小实例S1 简易学生选课管理系统 第8节--CSS样式优化 点击查看教程总目录 作者自我介绍:b站小UP主,时常直播编程+红警三,python1对1辅导老师. 前面的几节下来,用户模块基 ...

  10. AtCoder Regular Contest 127

    Portal B Description 给出\(n(\leq5\times10^4),L(\leq15)\),构造\(3n\)个不同\(L\)位的三进制数,使得在这\(3n\)个数的每一位上,0/1 ...