【LeetCode】170. Two Sum III - Data structure design 解题报告(C++)
2024-10-17 10:50:40
- 作者: 负雪明烛
- 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 日 —— 举杯邀明月,对影成三人
最新文章
- 8.3 使用Fluent API进行属性映射【Code-First系列】
- Atitit。数据库 安全性 重要敏感数据加密存储解决方案
- js创建标签的方法--依赖于jquery
- 为luna版本eclipse增加Web插件
- tomcat 内存问题 xms xmx permsize maxPermsize
- input输入样式,动画
- 【原】textarea 换行之间的转换
- (转载)移动WEB前端开发资源整合
- ZZmsvcprt.lib(MSVCP90.dll) : error LNK2005:已经在libcpmtd.lib(xmutex.obj) 中定义 .的分析解决办法 (转)
- Selenium2学习-029-WebUI自动化实战实例-027-判断元素是否存在
- 用SQLData读写数据库自定义类型
- ThinkPHP的缓存 F方法
- Python中,如何初始化不同的变量类型为空值
- Extjs视频
- APP被苹果APPStore拒绝的各种原因
- 第六百二十七天 how can I 坚持
- js监听全屏下的esc事件
- centos6.5下安装Nginx
- Zabbix4.0+第三方报警平台OneAlert监控报警
- SVN Update Error: Please execute the &#39;Cleanup&#39; command