【題目描述】
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.
Notice: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.
給出一個有n個整數(shù)的數(shù)組S供填,在S中找到三個整數(shù)a, b, c粱年,找到所有使得a + b + c = 0的三元組。
注意:在三元組(a, b, c)熬甚,要求a <= b <= c。結(jié)果不能包含重復(fù)的三元組聊品。
【題目鏈接】
?http://www.lintcode.com/en/problem/3sum/
【題目解析】
方法一:
兩層循環(huán)+二分查找飞蹂,復(fù)雜度O(n^2 logn). 太慢了
方法二:??
一層循環(huán)加two sum思想(https://leetcode.com/problems/two-sum/),O(n^2).
【參考答案】