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. Oracle学习第二天---Profile的使用

    环境:Oracle 11g 一.权限的传递 1.如果传递的是对象权限,就在后面加入with grant option: eg:system用户有张temp的表,只想usertest用户拥有查询的权限, ...

  2. iOS预处理指令

    预处理过程扫描源代码,对其进行初步的转换,产生新的源代码提供给编译器.可见预处理过程先于编译器对源代码进行处理. 预处理指令是以#开头的代码行,#后是指令关键字,在关键字和#号之间允许存在任意个数的空 ...

  3. (转)Web2.0 大型互联网站点的架构

    这种资料.向来可遇不可求啊 WikiPedia 技术架构学习分享 http://www.dbanotes.net/opensource/wikipedia_arch.html YouTube 的架构扩 ...

  4. (UVALive 7261)Xiongnu's Land 二分

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  5. vs2005_the breakpoint will not currently be hit. The source code is different from the original verison.

    公司开发只能用英文版的VS2005 (雅蠛蝶) Tool-->Options-->Debugging->General: 把Require source files to exact ...

  6. Linux 下面对物理地址的访问

    参考链接:http://zhuhaibobb.blog.163.com/blog/static/2744006720101049030606/ Linux内核提供了/dev/mem驱动,提供了一种直接 ...

  7. C# 实现HTML5服务器推送事件

    为什么需要服务器推送事件: 因为如果需要保持前台数据的实时更新例如,IM聊天,股票信息, 1.可以在客户端不断地调用服务端的方法来获得新数据,但是这样会很消耗服务器资源,导致系统变慢! 2 html5 ...

  8. ListView 复制到剪切板

    private void 导出ToolStripMenuItem_Click(object sender, EventArgs e) { Clipboard.SetText(GetListView(l ...

  9. BroadcastReceiver

    BroadcastReceiver 广播接受者 Android中, Broadcast是一种数据传递的方式/通信方式. Brodadcast 是Android 系统组件之一 广播的特性 1. 由一个发 ...

  10. mongodb 级联操作查询时,关联条件

    ObjectId id=new ObjectId("123"); c=c.where("relation_type.$id").is(id);