Given an array S of n integers, are there elements x, y, z in S such that x + y + z = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.

    一个重要的优化就是如果上个数已经求过集合了 例如[-1 -1 0 2 2]已经包含了[-1 0 2 2] 当然也包含了 [-1 0 2]集合的。
Posted on 2012-12-10 11:38 micheal's tech 阅读(187) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理