Algorithms - Priceton - 02 - 3-Sum

3-Sum : Given N distinct integers, how many triples sum to exactly zero?

---------------------------------------------------------------------------------------

3-Sum : brute-force algorithm O(N3)

---------------------------------------------------------------------------------------

3-Sum : sorting-based algorithm O(N2lgN)

  • sort the numbers.O(NlgN)
  • for each pair of numbers a[i] and a[j], binary search for -(a[i]+a[j]). O(N2lgN)

posted on 2013-10-06 21:32  haoyancoder  阅读(133)  评论(0)    收藏  举报

导航