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.net自定义控件

    回发星级控件 using System; using System.ComponentModel; using System.Web.UI; using System.Web.UI.WebContro ...

  2. HDOJ2025查找最大元素

    查找最大元素 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. Sql传参含有单引号

    程序 exec heduling_date_select ' Name like @%a%@ or phone like @%a%@ or Cus_code like @%a%@ or objjc l ...

  4. AngularJS 学习随笔(一)

    AngularJS 初始化加载流程: 1:浏览器载入HTML,然后把它解析成DOM 2:浏览器载入Angular.JS 脚本 3:AngularJS 等到DOMContentLoaded时间触发 4: ...

  5. linux中vsftpd登陆慢卡问题解决方法

    1.修改服务器上的/etc/resolv.conf令其内容只有类似 nameserver 192.81.133.229 nameserver 114.114.114.114 出现问题的机器的resol ...

  6. JavaScript动画附源码(一)

        JavaScript完成动画程序 1,效果图: 以上是纯CSS+JavaScript实现的.点击关闭按钮可以动态关闭这个方框.兼容IE/FF/Chrome.这样的效果如果用jquery实现起来 ...

  7. 第十篇、微信小程序-view组件

    视图容器 常用的样式的属性: 详情:http://www.jianshu.com/p/f82262002f8a display :显示的模式.可选项有:flex(代表view可以伸缩,弹性布局)- f ...

  8. 20141014--判断语句switch case

    1. switch case break(跳出) 距离break 最近的大括号,跳出这个大括号,执行大括号之后的命令 switch case 必须与 break 一同使用. 分别输入月份 几号 输出是 ...

  9. javascript笔记——js的阻塞特性[转载]

    JS具有阻塞特性,当浏览器在执行js代码时,不能同时做其它事情,即<script>每次出现都会让页面等待脚本的解析和执行(不论JS是内嵌的还是外链的),JS代码执行完成后,才继续渲染页面. ...

  10. GIS初学者

    学习编程一直以来没有什么好的思路,感觉就是学了忘,忘了再重复,效率特别低下.大概是从大三第一学期才有意识的转向c#的学习,来熟悉VS2010平台,在这之前我都不知道自己是怎么学习的. 大一第二学期开的 ...