【题目】

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:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
  • The solution set must not contain duplicate quadruplets.
    For example, given array S = {1 0 -1 0 -2 2}, and target = 0.

    A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)

【解析】

3Sum3Sum Closest 的扩展,相同思路,加强理解。

K Sum 问题的时间复杂度好像为 O(n^(k-1)) ?!假设有更好的,欢迎不吝赐教!

【Java代码】

public class Solution {
List<List<Integer>> ret = new ArrayList<List<Integer>>(); public List<List<Integer>> fourSum(int[] num, int target) {
if (num == null || num.length < 4) return ret;
Arrays.sort(num);
int len = num.length;
for (int i = 0; i < len-3; i++) {
if (i > 0 && num[i] == num[i-1]) continue;
for (int j = i+1; j < len-2; j++) {
if (j > i+1 && num[j] == num[j-1]) continue;
findTwo(num, j+1, len-1, target, num[i], num[j]);
}
}
return ret;
} public void findTwo(int[] num, int begin, int end, int target, int a, int b) {
if (begin < 0 || end >= num.length) return;
int l = begin, r = end;
while (l < r) {
if (a+b+num[l]+num[r] < target) {
l++;
} else if (a+b+num[l]+num[r] > target) {
r--;
} else {
List<Integer> ans = new ArrayList<Integer>();
ans.add(a);
ans.add(b);
ans.add(num[l]);
ans.add(num[r]);
ret.add(ans);
l++;
r--;
while (l < r && num[l] == num[l-1]) l++;
while (l < r && num[r] == num[r+1]) r--;
}
}
}
}

【LeetCode】4Sum 解题报告的更多相关文章

  1. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  2. leetcode—Palindrome 解题报告

    1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...

  3. LeetCode C++ 解题报告

    自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...

  4. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  5. LeetCode: Subsets 解题报告

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  6. LeetCode: Triangle 解题报告

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  7. LeetCode: isSameTree1 解题报告

    isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary t ...

  8. LeetCode: Combinations 解题报告

    Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...

  9. LeetCode: solveSudoku 解题报告

    Sudoku SolverWrite a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are in ...

随机推荐

  1. TCP和HTTP

    TCP和HTTP 2013-11-01 11:29 6564人阅读 评论(2) 收藏 举报 分类: 计算机—杂七杂八(15) 1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议, ...

  2. Matalab之模糊KMeans实现

    这节继续上节的KMeans进行介绍,上节主要是对模糊KMeans方法的原理做了介绍,没有实践印象总是不深刻,前段时间有个师姐让我帮着写了个模糊KMeans的算法,今天就拿她给出的例子来对这个方法做个实 ...

  3. Chapter 4. Button, Checkbutton, and Radiobutton Widgets 按钮,复选按钮,单选按钮

    Chapter 4. Button, Checkbutton, and Radiobutton Widgets   按钮,复选按钮,单选按钮 几乎所有的Perl/Tk 应用使用按钮以这样或者那样的方式 ...

  4. SonarQube代码质量管理平台工具

    1.Sonar轮廓介绍 Sonar (SonarQube)是一个开源平台,用于管理源代码的质量.Sonar 不只是一个质量数据报告工具,更是代码质量管理平台.支持的语言包括:Java.PHP.C#.C ...

  5. CentOS7 yum lamp 虚拟主机配置 lamp各组件简单影响性能的参数调整--for 一定的环境需求

    LAMP Server on CentOS 7 Updated Tuesday, January 13, 2015 by Joel Kruger This guide provides step-by ...

  6. SharePoint 2010 用Event Receiver将文件夹自动变成approved状态 (2)

    接上篇,先贴ItemUpdated的代码: base.ItemUpdated(properties); if (properties.ListItem.FileSystemObjectType != ...

  7. SQL Server 事务嵌套

    示例代码: DECLARE @TranCounter INT; SET @TranCounter = @@TRANCOUNT; -- Procedure called when there is -- ...

  8. SSR服务端一键安装脚本

    支持新协议混淆,SSR服务端一键安装脚本   Shadowsocks-R 是项目 shadowsocks 的增强版,用于方便地产生各种协议接口.实现为在原来的协议外套一层编码和解码接口,不但可以伪装成 ...

  9. Android 大神博客汇集

    非常给力的CSDNBlog和个人Blog,这些Blog都有一个共同的特点,即内容详实,讲解透彻,也算是给后来的初学者指一条路吧!只要你下定决心跟随强者的脚步,成为人们眼中的大神,只不过是时间问题! 下 ...

  10. js统计字符串,并且判断出现次数最多的

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...