LeetCode 15 3Sum
来源:互联网 发布:淘宝店铺四个钻石 编辑:程序博客网 时间:2024/06/14 07:53
题意:
求不重复的使得x + y + z = 0的(x, y, z)组合。
思路:
为了防止重复可以按x <= y <= z来枚举,z可以用map来查找。
我的代码好慢…不知道是不是用了long long的缘故…求快速的代码??
代码:
//// Created by house on 1/9/17.//class Solution {public: vector <vector<int>> threeSum(vector<int> &nums) { vector <vector<int>> ans; map<long long, int> cnt; for (int x : nums) { ++cnt[x]; } for (map<long long, int>::iterator it1 = cnt.begin(); it1 != cnt.end(); ++it1) { int x = it1->first; --cnt[x]; for (auto it2 = it1; it2 != cnt.end(); ++it2) { if (it2->second) { int y = it2->first; --cnt[y]; long long sum = (long long) x + y; if (-sum >= y && cnt.count(-sum) && cnt[-sum] > 0) { vector<int> tmp{x, y, -sum}; ans.push_back(tmp); } ++cnt[y]; } } ++cnt[x]; } return ans; }};
0 0
- Leetcode 15 3Sum
- LeetCode 15: 3Sum
- leetcode 15 3Sum
- [leetcode 15] 3Sum
- Leetcode【15】:3Sum
- [leetcode 15] 3Sum
- [Leetcode] 15 - 3Sum
- leetcode.15-----------3Sum
- leetcode 15 3Sum
- leetcode-15 3Sum
- 3Sum - LeetCode 15
- Leetcode #15 3 Sum
- LeetCode---(15) 3 sum
- LeetCode 15 - 3Sum
- leetcode-15 3sum
- leetcode 15 -- 3Sum
- Leetcode[15]-3Sum
- LeetCode 15: 3 Sum
- class字节码类结构
- 配置php
- 交换排序
- 使用java实现cmd操作
- mysql中查询某个数据库的username
- LeetCode 15 3Sum
- SSH远程拷贝文件
- 存储过程详解
- Linux下的Memcache安装及安装Memcache的PHP扩展安装
- 数据中心网络里的Underlay和Overlay
- VisualVM远程连接并监控服务器上的jvm进程
- iOS程序设计心得总结(二)网络层设计
- iOS build 与version,InfoDictionary version的区别
- Android 热修复框架 Tinker ( 三 )