前言
3Sum算是LeetCode最经典的十几道题之一了,据说在面试中出现的频率相当高。所以在这里花点篇幅讨论一下此题。
题目
https://leetcode.com/problems/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:
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.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
题目大意就是在给定的数组中找出三个数字,使得它们的和为零。把所有满足此条件且不重样的一组数字塞进vector< vector >里并返回之。