【题目】

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. 使用Node.js快速搭建WebSocket server

    原文地址:http://my.oschina.net/yushulx/blog/309413 目录[-] 安装 服务端 客户端 参考 安装 ? 1 npm install ws 服务端 server. ...

  2. Asp.Net构架(Http请求处理流程)、Asp.Net 构架(Http Handler 介绍)、Asp.Net 构架(HttpModule 介绍)

    转载: HttpHaddler,HttpModule http://blog.csdn.net/jiuqiyuliang/article/details/18713451 http://www.cnb ...

  3. vmware重装系统后虚拟机实例文件*.vmdk重用

    如题:vmware重装系统后自定义的:虚拟机名称*.vmdk文件重用. 一.问题描述 系统磁盘坏道,装不上系统直接换了硬盘,但是新装的Vmware不能够通过open方式打开“自定义*.vmdk”(这个 ...

  4. centos Ddos防范开源软件使用及apache ab压测 测试

    1,检索特定字符的相同行,用于过滤点一些命令行的头说明

  5. ARM64调试环境

    自从上一次ZCTF做了一道ARM64的逆向题目后,我决定记录下利用qemu搭建ARM64的环境的过程,以后肯定会遇到更多ARM平台下的Reverse和PWN. 一 安装QEMU 我要模拟的是64位的A ...

  6. DictoryInfo.GetFiles

    using System; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(stri ...

  7. 修改MyEclipse8.6的Servlet模板

    D:\Genuitec\Common\plugins\com.genuitec.eclipse.wizards_****.jar 解压jar包, 进入templates文件夹,修改生成Servlet模 ...

  8. mysql 重启

    /etc/init.d/mysql restart /etc/init.d/mysql stop /etc/init.d/mysql start

  9. 前端开发的常用js库

    验证: jQuery formValidator,Validform; 提示框: artDialog, lhgDialog,jBox,jQuery textbox plugin 文件批量上传:uplo ...

  10. js数组的操作<转>

    转自 http://blog.csdn.net/xcxinghai/article/details/13502583 PS(个人理解): 1) 数组项的数据类型可以是混合多样的,同时可以含string ...