3 sum

3 sum_第1张图片


Solution:

Sort then using two pointers, one at the beginning element, one at the end element, moving towards, calculate the sum of three elements.

time: O(N^2) , space: O(1)



3 sum_第2张图片

 总结:

考虑non-descending,先sort数组,其次考虑去除duplicate; 类似2sum,可以利用two pointers,不断移动left 和 right 指针,直到找到目标,或者两指针相遇


update:

 用hashSet 去重, 增加 O(N)  空间

你可能感兴趣的:(3 sum)