LeetCode 15 3Sum 题解
1.题目:
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]
]
题意:
输入一个数组,找出所有某3个数加起来和为0的组合。
2.解题思路:
悲伤望天!!!写了好久!!!人肉debug!!最后发现超时!!继续折腾!!折腾了好几种!!又回来。。终于没超时了TUT。
以上是心路历程。。具体与2sum差不多,不过两层循环,left,right向中间查找,注意去重的循环,不加会超时。听说可以二分,第一次尝试失败之后,已躺尸,待填TUT
3.代码
1 |
|