leetcode3 Two Sum III – Data structure design
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的更多相关文章
- 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 - ...
- 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 - ...
随机推荐
- response小结(四)
1.发送http头,控制浏览器定时刷新网页(REFRESH). package com.yyz.response; import java.io.IOException; import javax.s ...
- 重叠I/O之可等待的重叠I/O【系列一】
一 什么是异步I/O 同步I/O和异步I/O的关键不同就是在发出I/O请求后,线程是否会阻塞.当线程发出一个设备I/O请求的时候,线程会被挂起来,直到设备完成I/O请求为止,这称之为同步I/O.而对于 ...
- Appcn 移动开发 前台与服务器数据交互
第一次写.嘿嘿. 言归正传,这几天开始学习移动开发,使用的是Appcan平台.Appcan平台采用HTML5+CSS3做开发 实现跨平台,正好可以满足我们的业务需求. Appacn和数据库进行交互的方 ...
- 使用gulp脚本配合TypeScript开发
目标:编写TypeScript时,保存即生成js文件. 使用npm安装以下组件 gulp gulp-rename through-gulp gulp-typescript 编写gulpfile ...
- Cocos2d-x中SQLite数据库管理工具
数据库创建完成后,我们可能需要看看数据库中数据是否成功插入,很多人喜欢使用图形界面工具来管理SQLite数据库.SQLite图形界面管理工具有很多,我推荐使用SQLiteStudio工具,下载地址ht ...
- (poj 3177) Redundant Paths
题目链接 :http://poj.org/problem?id=3177 Description In order to <= F <= ,) grazing fields (which ...
- ZigBee 入网详解
本文将根据Sniffer来详细解释ZigBee终端设备入网的整个流程,原创博文. 当协调器建立好网络后,终端设备执行zb_startrequest函数,准备入网时,他们两者之间详细的流程如下.
- ◆◆◆◆◆◆◆◆◆◆◆linux下软件包的管理◆◆◆◆◆◆◆◆◆◆◆◆◆◆
查看与制定的路径名相匹配的软件包 [root@localhost certs]# which ls alias ls='ls --color=auto' /bin/ls [root@localhost ...
- Linux--fedora21 PC机安装以及拨号上网和无限上网
最近回家,学习许久未用的linux,之前也是在培训的时候用的是 ubuntu ,这次回家查了下 fedora 最适合开发人员使用,所以就装了个试试.刚开始只能拨号上网,经过三天时间的各种搜索查找终于解 ...
- 提升PHP性能的21种方法
提升PHP性能的21种方法. 1.用单引号来包含字符串要比双引号来包含字符串更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会.2.如果能将类的方法定义成static,就尽量定义成st ...