【leetcode】3Sum (medium)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- The solution set must not contain duplicate triplets.
思路:
把最小值从头到尾循环,中间值和最大值两边收缩。
我写的时候是在最后去重复,总是超时。后来看了人家的答案,发现可以每次对最小、中间、最大值去重。就可以AC了
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <queue>
- #include <stack>
- using namespace std;
- class Solution {
- public:
- vector<vector<int> > threeSum(vector<int> &num) {
- vector<vector<int> > ans;
- if(num.size() < )
- {
- return ans;
- }
- int small = ;
- int big = num.size() - ;
- int mid = ;
- int sum = ;
- sort(num.begin(), num.end());
- for(small = ; small < num.size() - ; /*small++*/) //最小数字循环 中间与最大数字两边收缩
- {
- mid = small + ;
- big = num.size() - ;
- while(mid < big)
- {
- sum = num[small] + num[mid] + num[big];
- if(sum > )
- {
- big--;
- }
- else if(sum < )
- {
- mid++;
- }
- else
- {
- vector<int> v;
- v.push_back(num[small]);
- v.push_back(num[mid]);
- v.push_back(num[big]);
- ans.push_back(v);
- do { mid++; }while (mid < big && num[mid - ] == num[mid]); //注意!!
- do { big--; }while (mid < big && num[big + ] == num[big]);//注意!!
- }
- }
- do{ small++; }while (small < num.size() - && num[small - ] == num[small]);//注意!!
- }
- return ans;
- }
- };
- int main()
- {
- Solution s;
- vector<int> num;
- num.push_back(-);
- num.push_back(-);
- num.push_back(-);
- num.push_back(-);
- num.push_back(-);
- num.push_back();
- num.push_back();
- num.push_back();
- num.push_back();
- num.push_back();
- vector<vector<int>> ans = s.threeSum(num);
- return ;
- }
【leetcode】3Sum (medium)的更多相关文章
- 【LeetCode】3Sum 解决报告
这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- 【leetcode】Subsets (Medium) ☆
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 【leetcode】3Sum
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- 【leetcode】3Sum Closest(middle)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
随机推荐
- IOS开发之----NSDictionary,JSON和XML互相转换
本文永久地址为 http://www.cnblogs.com/ChenYilong/p/4044521.html,转载请注明出处. -(void)test { //XML文本范例 ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- 2015Summer Training #2
暑假集训昨天刚开始,14级的小伙伴快到齐了,hhhhh ,毕竟下学期区域赛,对我们来说还是很困难的. 打算每天写份总结. UVA 11300 C.Spreading the Wealth 题目大意:n ...
- jquery mobile cannot be created in a document with origin 'null' and URL
jquery mobile cannot be created in a document with origin 'null' and URL http://zhidao.baidu.com/lin ...
- 如何在CentOS 7上安装EPEL源
EPEL 是什么? EPEL (Extra Packages for Enterprise Linux,企业版Linux的额外软件包) 是Fedora小组维护的一个软件仓库项目,为RHEL/CentO ...
- SQL server 语句新建用户、对用户授权、删除用户实例
Grant select on tb to db_user --给db_user用户授权 tb表 查询权限 一.命令操作 USE mydb GO --1. 新建测试用户 --1.1 添加登录用户和密码 ...
- BZOJ3052——糖果公园
0.题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3052 1.题目大意:给定一颗n个点的无根树,每个点有一个颜色,要进行q次操作,有两种操 ...
- python os.path模块常用方法详解:转:http://wangwei007.blog.51cto.com/68019/1104940
1.os.path.abspath(path) 返回path规范化的绝对路径. >>> os.path.abspath('test.csv') 'C:\\Python25\\test ...
- linux kernel 平台总线实例分析
linux 平台总线的实现有三大块 , platform bus , platform device , platform drvice 平台类型结构体: /** * struct bus_type ...
- POJ 2631 DFS+带权无向图最长路径
http://poj.org/problem?id=2631 2333水题, 有一个小技巧是说随便找一个点作为起点, 找到这个点的最远点, 以这个最远点为起点, 再次找到的最远点就是这个图的最远点 证 ...