[leetcode 刷题记录] 170. Two Sum III - Data structure design
2016-07-11 09:59
309 查看
170. Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations:
For example,
这个solution用到了multiset,multisite允许set中出现重复元素
class TwoSum {
public:
// Add the number to an internal data structure.
void add(int number) {
nums.insert(number);
}
// Find if there exists any pair of numbers which sum is equal to the value.
bool find(int value) {
for (auto n: nums) {
int count = value - n == n ? 2 : 1;
if (nums.count(value - n) >= count) return true;
}
return false;
}
private:
unordered_multiset<int> nums;
};
Design and implement a TwoSum class. It should support the following operations:
addand
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.
For example,
add(1); add(3); add(5); find(4) -> true find(7) -> false
这个solution用到了multiset,multisite允许set中出现重复元素
class TwoSum {
public:
// Add the number to an internal data structure.
void add(int number) {
nums.insert(number);
}
// Find if there exists any pair of numbers which sum is equal to the value.
bool find(int value) {
for (auto n: nums) {
int count = value - n == n ? 2 : 1;
if (nums.count(value - n) >= count) return true;
}
return false;
}
private:
unordered_multiset<int> nums;
};
相关文章推荐
- cf 87 A
- NO.163 如何做禅道二次开发(3):禅道的数据库结构
- TeeChart曲线平滑 Line.Smoothed
- dfs连通块2
- C++代码命名规范约定
- ACM刷题之HDU————How Many Trees?
- paramiko建立无密码传输认证
- CocoaPods 命令和使用
- Servlet开发(二)
- ul li设置横排,并除去li前的圆点
- 使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解
- C++中namespace和class的区别
- Action的生命周期
- java操作Excel
- 类似直播app点赞动画分析
- 使用微信 SDK 上传图片到七牛
- 远程桌面连接超出最大连接数解决办法
- HDU 5492 Find a path DP —— 2015 ACM-ICPC Asia Regional Hefei Online
- 字符串排序
- AFNetworking Error Domain=NSCocoaErrorDomain Code=3840