3sum 三数之和为0的简单实现
来源:互联网 发布:龙虎榜数据准确吗 编辑:程序博客网 时间:2024/04/29 20:36
思路简单:
(1) 三重 for ,符合a+b+c=0的 a,b,c保存在tuple里
(2)tuple保存在set 中,一可去重,二可保持字典序
(3)简单代价就是复杂度很高,O(n^3*logn)
typedef tuple<int,int,int> triplet;triplet sort3(int a,int b,int c){if(a<=b){if(b>c){swap(b,c);if(a>b) swap(a,b);}}else{swap(a,b);if(b>c){swap(b,c);if(a>b) swap(a,b);}}triplet res = make_tuple(a,b,c);return res;}vector<vector<int> > threeSum(vector<int> &num) { auto sz= num.size(); triplet t3; set<triplet> set3; vector<vector<int> >res; vector<int> mid; if(sz<3) return res; for(auto i = 0 ; i<sz-2 ; ++i) for(auto j=i+1 ; j<sz-1 ; ++j ) for (auto k=j+1; k<sz ;++k) if(num[i]+num[j]+num[k]==0){ t3 = sort3(num[i],num[j],num[k]); set3.insert(t3); } for(auto a:set3){ mid.clear(); mid.push_back(get<0>(a)); mid.push_back(get<1>(a)); mid.push_back(get<2>(a)); res.push_back(mid); } return res;}
0 0
- 3sum 三数之和为0的简单实现
- 三数之和为0的三指针实现
- 3sum 三个数之和为0
- 三数之和为0的组合
- 3Sum Closest(离目标值最近的三数之和)
- [LeetCode] 3Sum Closest 最近的三数之和 Python
- 3Sum 数组找3数之和为0 @LeetCode
- Leetcode: 15. 3Sum 三数之和
- leetcode-3sum 三数之和
- [LeetCode] 3Sum 三数之和 Python
- 关于三个数之和为0的最简单解法
- 3Sum(数组中三个数之和为零)
- Leetcode #15 3Sum 三数之和 解题小节
- LeetCode 15. 3Sum(三数之和)
- LeetCodet题解--16. 3Sum Closest(离给定值最近的三数之和)
- [Leetcode P15] Three Sum 三数之和
- 3Sum 三个数之和
- 【lintcode】两数之和、三数之和、最接近的三数之和、四数之和小结
- Min Stack
- 在部署MVC4.0网站的过程中遇到的问题以及解决办法
- 噪声及滤波小结
- 关于自动布局的文章
- /boot partition is full
- 3sum 三数之和为0的简单实现
- 【实战】sphinx索引合并的脚本
- php版本历史
- strings in documents must be valid UTF-8: '\xbf\xcd\xbb\
- JBOSS7编码设置(解决中文乱码有关)
- conversion to dalvik format failed with ...
- HealthKit框架指南
- Failed to instantiate the default view controller for UIMainStoryboardFile
- DWR简单实例