【LeetCode】170. Two Sum III - Data structure design 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址: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++)的更多相关文章
- 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 - ...
- ✡ 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[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两数之和III - 数据结构设计
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- 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】170. Two Sum III – Data structure design
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- 【leetcode】170. 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 ...
随机推荐
- 水平梯度在sigma坐标对应形式
sigma 坐标变换 一般 \(\sigma\) 坐标转换方程为 \[\sigma = \frac{z-\eta}{D} = \frac{z-\eta}{H+\eta} \] 转换后水深 z 范围由原 ...
- perl练习——FASTA格式文件中序列GC含量计算&perl数组排序如何获得下标或者键
一.关于程序: FUN:计算FASTA文件中每条序列中G和C的含量百分比,输出最大值及其id INPUT:FASTA格式文件 >seq1 CGCCGAGCGCTTGACCTCCAGCAAGACG ...
- 12. Fedora 中文乱码问题
1. Rhythmbox(音乐播放器乱码) yum install python-mutagen mid3iconv -e GBK *.mp3 2. totem电影播放机播放列表乱码解决1).修改to ...
- Linux之sftp服务
Linux之sftp服务 一.sftp介绍转自:[1]Linux如何开启SFTP https://www.cnblogs.com/xuliangxing/p/7120205.htmlSFTP是Secu ...
- vue2 页面路由
vue官方文档 src/views/Login.vue <template> <div> <h2>登录页</h2> </div> </ ...
- my42_Mysql基于ROW格式的主从同步
模拟主从update事务,从库跳过部分update事务后,再次开始同步的现象 主库 mysql> select * from dbamngdb.isNodeOK; +----+--------- ...
- jstl中的choose标签
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ ta ...
- 3.使用Spring Data ElasticSearch操作ElasticSearch(5.6.8版本)
1.引入maven坐标 <!--spring-data-elasticsearch--><dependency> <groupId>org.springframew ...
- 【Python】matplotlib直方图纵轴显示百分比
其实很简单,就是算了一下百分比权重,乘以了一个权重值 import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatt ...
- 『与善仁』Appium基础 — 24、等待activity出现
目录 1.什么是等待activity出现 2.wait_activity()方法 3.获取当前页面的activity方法 4.综合练习 1.什么是等待activity出现 在启动APP的时候,要配置包 ...