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.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.
1 | A solution set is: |
3sum的最优解时间复杂度是O(N2)的,4sum如果建立在3sum之上,最优解的时间复杂度是O(N3),实现代码如下
1 | class Solution { |