题目地址:https://leetcode-cn.com/problems/two-sum-iii-data-structure-design/

题目描述

Design and implement a TwoSum class. It should support the following operations: add and find.

  • add - Add the number to an internal data structure.
  • find - Find if there exists any pair of numbers which sum is equal to the value.

Example 1:

add(1); add(3); add(5);
find(4) -> true
find(7) -> false

Example 2:

add(3); add(1); add(2);
find(3) -> true
find(6) -> false

题目大意

设计并实现一个 TwoSum 的类,使该类需要支持 add 和 find 的操作。

解题方法

数组+字典

使用字典保存每个数字出现的位置,并且另外使用一个数组按照顺序保存出现的数字。查找的时候从左向右遍历数组中的每个数字left,查找value - left是否在字典中,并且和left位置不同。

C++代码如下:

class TwoSum {
public:
/** Initialize your data structure here. */
TwoSum() {
} /** Add the number to an internal data structure.. */
void add(int number) {
nums.push_back(number);
m[number] = nums.size() - 1;
} /** Find if there exists any pair of numbers which sum is equal to the value. */
bool find(int value) {
for (int i = 0; i < nums.size(); ++i) {
int left = nums[i];
int right = value - left;
if (m.count(right) && m[right] != i)
return true;
}
return false;
}
private:
map<int, int> m;
vector<int> nums;
}; /**
* Your TwoSum object will be instantiated and called as such:
* TwoSum* obj = new TwoSum();
* obj->add(number);
* bool param_2 = obj->find(value);
*/

平衡查找树+双指针

这个题和1. Two Sum是类似的,我使用的双指针的方法,那么要求已经插入的数据是有序的,所以使用了平衡查找树(红黑树,在C++中是map),保存每个数字出现的次数。

每次find的时候,从左右两个位置向中间走,如果左右指针的和是target说明找到了;如果和比target大,右指针向左移动;如果和比target小,左指针向右移动。

由于可能会存在重复的数字,所以map中是保存的数字出现的次数。查找到target的条件是:左右指针不相等且和等于target 或者 左右指针相等且和等于target且该数字出现的次数不止1次。

C++代码如下:

class TwoSum {
public:
/** Initialize your data structure here. */
TwoSum() {
} /** Add the number to an internal data structure.. */
void add(int number) {
m[number] ++;
} /** Find if there exists any pair of numbers which sum is equal to the value. */
bool find(int value) {
if (m.empty()) return false;
auto left = m.begin();
auto right = m.end();
right --;
while (left != right) {
int cur_sum = left->first + right->first;
if (cur_sum == value
&& (left != right || left->second > 1))
return true;
else if (cur_sum > value)
right --;
else
left ++;
}
return left->first + right->first == value && (left != right || left->second > 1);
}
private:
map<int, int> m;
}; /**
* Your TwoSum object will be instantiated and called as such:
* TwoSum* obj = new TwoSum();
* obj->add(number);
* bool param_2 = obj->find(value);
*/

日期

2019 年 9 月 19 日 —— 举杯邀明月,对影成三人

【LeetCode】170. Two Sum III - Data structure design 解题报告(C++)的更多相关文章

  1. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  2. [LeetCode] 170. 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[170]Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  5. [leetcode]170. Two Sum III - Data structure design两数之和III - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  6. 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 ...

  7. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  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. 170. Two Sum III - Data structure design

    题目: Design and implement a TwoSum class. It should support the following operations: add and find. a ...

随机推荐

  1. 微信第三方平台获取component_verify_ticket

    官方文档说明: 在公众号第三方平台创建审核通过后,微信服务器会向其"授权事件接收URL"每隔10分钟定时推送component_verify_ticket.第三方平台方在收到tic ...

  2. 【模板】无源汇有上下界可行流(网络流)/ZOJ2314

    先导知识 网络最大流 题目链接 https://vjudge.net/problem/ZOJ-2314 题目大意 多组数据,第一行为数据组数 \(T\). 对于每一组数据,第一行为 \(n,m\) 表 ...

  3. 自动化测试系列(三)|UI测试

    UI 测试是一种测试类型,也称为用户界面测试,通过该测试,我们检查应用程序的界面是否工作正常或是否存在任何妨碍用户行为且不符合书面规格的 BUG.了解用户将如何在用户和网站之间进行交互以执行 UI 测 ...

  4. 学习java的第二十五天

    一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...

  5. flink-----实时项目---day07-----1.Flink的checkpoint原理分析 2. 自定义两阶段提交sink(MySQL) 3 将数据写入Hbase(使用幂等性结合at least Once实现精确一次性语义) 4 ProtoBuf

    1.Flink中exactly once实现原理分析 生产者从kafka拉取数据以及消费者往kafka写数据都需要保证exactly once.目前flink中支持exactly once的sourc ...

  6. 顺序栈(C++)

    栈的定义为只允许在表的末端进行插入和删除的线性表.简而言之就是先进后出的线性表. 插入和删除的一端被称呼为栈顶(top),而不允许插入删除的一端被称为栈底(bottom).无元素时的栈即为空栈. 使用 ...

  7. Linux定时任务crontable简介

    Linux下定时执行任务的方法:Linux之crond 服务介绍:https://www.cnblogs.com/liang-io/p/9596294.html http://www.mamicode ...

  8. Oracle中的DBMS_LOCK包的使用

    一.DBMS_LOCK相关知识介绍 锁模式: 名字 描述 数据类型 值 nl_mode Null INTEGER 1 ss_mode Sub Shared: used on an aggregate ...

  9. LINUX 安装增强 前置安装文件

    yum install kernel yum install kernel-devel yum install gcc yum install make

  10. clickhouse安装数据导入及查询测试

    官网 https://clickhouse.tech/ quick start ubantu wget https://repo.yandex.ru/clickhouse/deb/lts/main/c ...