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 - ...
随机推荐
- IIS日志
1.认识IIS日志 IIS日志默认存放在System32\LogFiles目录下,使用W3C扩展格式.下面我们通过一条日志记录来认识它的格式 2005-01-0316:44:57218.17.90.6 ...
- Xcode7打包上传App Store发生ERROR ITMS-90535 错误解决方法
两个方法解决 1.如果用不到QQ分享啥的,直接删掉TencentOpenApi这个文件夹. 移除掉这个模块 ..简单粗暴,我就是这么解决的. 2.找到TencentOpenApi_IOS_Bundl ...
- (转)Facebook内部分享:26个高效工作的小技巧
春节假期马上就要结束了,该收收心进入新一年的工作节奏了~分享 26 个高效工作的小技巧,希望对大家有所帮助~(我发现自己只有最后一条执行得很好,并且堪称完美!) 1.时间常有,时间优先. 2.时间总会 ...
- Php 输出语句
输出语句 echo 示例: print(); 示例: 只能输出标量数据类型,无法输出布尔false print_r(); print_r可以以比较容易理解的方式显示数据 示例: var_dump(); ...
- oracle手动启动服务
开始-控制面板->管理工具->服务->右键属性 oracle服务设定都是手动启动的,开机时只需要启动3个服务就好了: OracleOraDb11g_home1ClrAgent Ora ...
- GDI+绘制文本
这是在论坛中有人提出的一个问题,原贴见:Graphics DrawString参数无效.这里给出方法,读者可以自行修改以适应自己的项目需求. 先上代码: if (!Page.IsPostBack) { ...
- IAR:Error [Li005]:no definition for"***" 问题之连接
对于 IAR 出现的 Error[Li005] 链接错误,网上已经给出了比较详尽的解决方法,而对于这次记录,主要是记录解决问题的思路. 网上给出的方法:http://blog.csdn.net/yue ...
- ASP.NET Web API标准的“管道式”设计
详见:http://www.cnblogs.com/artech/p/asp-net-web-api-pipeline.html http://www.codeproject.com/Articles ...
- 分享php中四种webservice实现的简单架构方法及实例[转载]
[转载]http://www.itokit.com/2012/0417/73615.html 本人所了解的webservice有以下几种:PHP本身的SOAP,开源的NUSOAP,商业版的PHPRPC ...
- sublime 设置localhost 2
最近sidebar用不了了,提示更新然后就自动卸载了: 研究了下其他方式实现: Sublime Text 2 Sublime Text 3 都可以使用: 菜单 --> Tools --> ...