题目地址: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. 【基因组组装】HiC挂载Juicebox纠错补充

    目录 1. 主要纠错类型 misjoins translocations inversions chromosome boundaries 2. 其他有用操作 撤销与反撤销 移到边角料 1. 主要纠错 ...

  2. 比对软件Blast,Blast+,Diamond比较

    1. Blast (1)格式化数据库 formatdb -i db.seq -p T -o T -l logfile 主要参数: -i 输入需要格式化的源数据库名称 -p 文件类型,是核苷酸序列数据库 ...

  3. [R] 如何快速生成许多差异明显的颜色?

    这个需求真的太常见了!注意问题强调的几个关键词:一是快速,二是大量,三是差异明显.在生成大量元素比较图时要明显区分不同样本,比如宏基因组中的物种分析: 方法一:自定义 自定义颜色:优点是选择差异明显的 ...

  4. Linux 进程与线程

    进程与线程   进程 进程就是在操作系统中运行的程序,是操作系统资源管理的最小单位.一个进程可以管理多个线程,线程相对轻量,可以共享进程地址空间 线程来源 一个进行在运行的过程中,不可能一直占据着CP ...

  5. python-django111111111111

    111 内置电池的意思就是,内置了很多功能,插件等等帮助文档:https://docs.djangoproject.com/en/3.0/ model,很多集成的东西,连接数据库等 vierm: Te ...

  6. R语言与医学统计图形-【31】动态交互绘图

    1.plotly包 动态散点图 library(plotly) # 交互散点图 plot_ly(data=iris, x=~Sepal.Length, y=~Petal.Length, marker= ...

  7. C#gridview尾部统计

    protected void gridSettlement_RowDataBound(object sender, GridViewRowEventArgs e) { if (dtSettlement ...

  8. 学习java的第六天

    一.今日收获 1.开始了学习手册第二章的学习 2.了解了java里的常量与变量以及数据类型,与c语言的内容类似 二.今日难题 1.都是基础知识,没有什么难题 三.明日目标 1.继续学习java学习手册 ...

  9. Spark(二十)【SparkSQL将CSV导入Kudu】

    目录 SparkSql 将CSV导入kudu pom 依赖 scala 代码 启动脚本 SparkSql 将CSV导入kudu pom 依赖 <properties> <spark. ...

  10. Spark基础:(二)Spark RDD编程

    1.RDD基础 Spark中的RDD就是一个不可变的分布式对象集合.每个RDD都被分为多个分区,这些分区运行在分区的不同节点上. 用户可以通过两种方式创建RDD: (1)读取外部数据集====> ...