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 - ...
随机推荐
- BUG修改纪录
刚进入现在的公司,接手了一个遗留的项目,BUG频出,最近一个星期都在加班改BUG,身心疲惫,为了 现在,将来不会再被相同BUG困扰,特来纪录一下. 1.数据库设计字段时,对于int等类型最好设置默认值 ...
- ADO.NET笔记(一)XML导入导出和数据库
数据导出成XML文件 #region 数据导出成XML文件 string sql = "select Id, CallerNumber, TelNum, StartDateTime, End ...
- 利用google api生成二维码名片例子
二维条码/二维码可以分为堆叠式/行排式二维条码和矩阵式二维条码.堆叠式/行排式二维条码形态上是由多行短截的一维条码堆叠而成:矩阵式二维条码以矩阵的形式组成,在矩阵相应元素位置上用“点”表示二进制“1” ...
- asp.net C#检查URL是否有效
我们有时候需要对用户输入的网站(URL)进行有效性检查, 代码如下 复制代码 function CheckUrl(str) { var RegUrl = new RegExp(); Re ...
- c# DataGridView操作
#region 操作DataGridView /// <summary> /// 初始化DataGridView属性 /// </summary> /// <param ...
- 关键字 static
A. 面向过程: 1. 静态全局变量:在全局变量前,加上关键词static,该变量就被定义成为一个静态全局变量. 1.1. 特点: |该变量在全局数据区分配内存: |未经初始化的静态全局变量会被程序自 ...
- Qt学习总结-ui篇(二)
qccs定义圆角 border-radius:10px; 如果想给特定位置定义圆角,如: 左上角:border-left-top-radius:10px; 右下角色:border-right-bo ...
- htm5实现视差动画
requestAnimationFrame.js window.requestAnimFrame = (function() { return window.requestAnimationFrame ...
- Linux 获取文件夹下的所有文件
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4129616.html #include <string> #include &l ...
- xml直接读取节点
<root> <books> <book id="one"></book> <book id="two"& ...