15. 3Sum
题目
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: The solution set must not contain duplicate triplets.For example, given array S = [-1, 0, 1, 2, -1, -4],A solution set is:[ [-1, 0, 1], [-1, -1, 2]]
解析
- 想写出一次能AC的代码真不容易!
- 很多细节问题,和sumtwo不一样的是:这次有重复元素;sumtwo假定没有重复元素,且只需要返回下标值
- 要跳过重复的元素
class Solution_15 {public: void twosum(vector >& vecs,vector & nums,int start, int target) { vector ans; int end = nums.size() - 1; while (start