您的位置:首页 > 其它

Leetcode 15 [medium]--3Sum

2018-02-04 23:22 393 查看
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 循环,会严重超时。先将array sort,设置一个for 循环,两个指针。三个数相加,根据和的大小调整指针的位置。最后注意避免因为array中的重复元素而造成的重复结果。如果元素与前一个元素相同,用continue 跳出该轮循环的剩余任务,继续进行下一轮循环(break 跳出整个循环)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: