首页 > 编程语言 > 详细

Java for LeetCode 018 4Sum

时间:2015-05-02 12:24:46      阅读:130      评论:0      收藏:0      [点我收藏+]

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

解题思路:

首先肯定是一次排序,如果是暴力枚举的话,肯定超时。因此,我们可以采用分治的思想,存储所有2个元素的和,然后采用2SUM的思路求解即可,这样时间复杂度不过O(n^2)

JAVA实现如下:

 

Java for LeetCode 018 4Sum

原文:http://www.cnblogs.com/tonyluis/p/4471779.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!