Leetcode 15 [medium]--3Sum

Given an array S of n integers, are there elements abc 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.

Leetcode 15 [medium]--3Sum

思路: 本题不可能设置三个for 循环,会严重超时。先将array sort,设置一个for 循环,两个指针。三个数相加,根据和的大小调整指针的位置。最后注意避免因为array中的重复元素而造成的重复结果。如果元素与前一个元素相同,用continue 跳出该轮循环的剩余任务,继续进行下一轮循环(break 跳出整个循环)

Leetcode 15 [medium]--3Sum