Question:

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

add(input) – Add the number input to an internal data structure.

find(value) – 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

我们把每个数字和其出现的次数建立映射,然后遍历哈希表,对于每个值,我们先求出此值和目标值之间的差值t,然后分两种情况来看,如果当前值不等于差值t,那么只要哈希表中有差值t就返回True,或者是当差值t等于当前值时,如果此时哈希表的映射次数大于1,则表示哈希表中还有另一个和当前值相等的数字,二者相加就是目标值。

class TwoSum {

private:

unordered_map<int,int> table;

public:

void add(int number) {

table[number]++;

}

bool find(int value) {

for(auto elem : table)

{

for (vector<string>::iterator i = v.begin(); i != v.end(); ++i);

for (auto i = v.begin(); i != v.end(); ++i);

int num1 = elem.second;

int num2 = value == (elem.first << 1) ? num1 - 1 : buffer.find(value - elem.first) != buffer.end();

if(num1 && num2){

return true;

}

}

return false;

}

};

leetcode3 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. 170. Two Sum III - Data structure design

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

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

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

  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. [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. asp中utf8不会出现乱码的写法

    <%@ CODEPAGE=65001 %> <% Response.CodePage=65001%> <% Response.Charset="UTF-8&qu ...

  2. dreamweaver中用正则表达式查找替换批量删除 tppabs标签的方法

    查找替换 正则表达式  \btppabs="h[^"]*" 后面不能有空格 你懂得的 选中右下角的 √[使用正则表达式] 替换全部

  3. C# 网页信息采集(数据访问)

    windows nt/xp/2003 or above .net Framework 1.1 SqlServer 2000 开发环境 VS 2003 目的 学习了网络编程,总要做点什么东西才好. 于是 ...

  4. dotnetbar 的BalloonTip的用法

    ‘设置提示标题 tip.SetBalloonCaption(txt_ID, "提示") ’设置显示的控件 和显示内容文本 tip.SetBalloonText(txt_ID, &q ...

  5. windows搭建virtualbox虚拟机安装的android环境

    1.首先安装virtualbox,从官网下载,安装完成之后在本地连接里面有virtualbox虚拟的网卡,可能会影响网络连接,一般禁用 2.下载android的镜像,完整名称是:android-x86 ...

  6. Java实战之04JavaWeb-03会话技术

    一.会话技术简介 1.什么是会话,为什么需要会话技术? 会话:从打开一个浏览器,访问某个网站,到关闭这个浏览器的这个过程称为一次会话.http协议是状态的. 2.会话技术的分类 客户端存储技术:Coo ...

  7. 09_Java8操作集合的一些新特性

    [使用forEach()结合Lambda表达式遍历集合] public class ForEachDemo { public static void main(String[] args) { Col ...

  8. linux 定时任务 crontab

    为当前用户创建cron服务 1.  键入 crontab  -e 编辑crontab服务文件 例如 文件内容如下: */2 * * * * /bin/sh /home/admin/jiaoben/bu ...

  9. C#如何获取快捷方式指向的目标文件

    前几天,做个小程序时遇到的问题,在百度上搜索了很久,没有发现好的解决方案,在英文网站上立刻搜到办法,看来还是国外的资源要全面一些,只是Google经常访问不正常,没办法啊. “C#如何获取快捷方式指向 ...

  10. linux 神器之wget

    1.什么是Wget? 首页,它是网络命令中最基本的.最好用的命令之一; 文字接口网页浏览器的好工具. 它(GNU Wget)是一个非交互从网上下载的自由工具(功能).它支持http.ftp.https ...