算法提高之LeetCode刷题

3 sum

2018-06-17  本文已影响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)

 总结:

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

update:

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

上一篇下一篇

猜你喜欢

热点阅读