3 sum

作者: 世界你好 | 来源:发表于2018-06-17 07:27 被阅读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)  空间

    相关文章

      网友评论

        本文标题:3 sum

        本文链接:https://www.haomeiwen.com/subject/imbieftx.html