【题目】

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.
  1. For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
  2.  
  3. A solution set is:
  4. (-1, 0, 0, 1)
  5. (-2, -1, 1, 2)
  6. (-2, 0, 0, 2)

【解析】

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

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

【Java代码】

  1. public class Solution {
  2. List<List<Integer>> ret = new ArrayList<List<Integer>>();
  3.  
  4. public List<List<Integer>> fourSum(int[] num, int target) {
  5. if (num == null || num.length < 4) return ret;
  6. Arrays.sort(num);
  7. int len = num.length;
  8. for (int i = 0; i < len-3; i++) {
  9. if (i > 0 && num[i] == num[i-1]) continue;
  10. for (int j = i+1; j < len-2; j++) {
  11. if (j > i+1 && num[j] == num[j-1]) continue;
  12. findTwo(num, j+1, len-1, target, num[i], num[j]);
  13. }
  14. }
  15. return ret;
  16. }
  17.  
  18. public void findTwo(int[] num, int begin, int end, int target, int a, int b) {
  19. if (begin < 0 || end >= num.length) return;
  20. int l = begin, r = end;
  21. while (l < r) {
  22. if (a+b+num[l]+num[r] < target) {
  23. l++;
  24. } else if (a+b+num[l]+num[r] > target) {
  25. r--;
  26. } else {
  27. List<Integer> ans = new ArrayList<Integer>();
  28. ans.add(a);
  29. ans.add(b);
  30. ans.add(num[l]);
  31. ans.add(num[r]);
  32. ret.add(ans);
  33. l++;
  34. r--;
  35. while (l < r && num[l] == num[l-1]) l++;
  36. while (l < r && num[r] == num[r+1]) r--;
  37. }
  38. }
  39. }
  40. }

【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. C语言的本质(20)——预处理之二:条件预处理和包含头文件

    我们可以通过定义不同的宏来决定编译程序对哪些代码进行处理.条件编译指令将决定那些代码被编译,而哪些是不被编译的.可以根据表达式的值或者某个特定的宏是否被定义来确定编译条件. 条件编译可分为三种情况,按 ...

  2. 推荐一款JavaScript日历控件:kimsoft-jscalendar

    一.什么是 kimsoft-jscalendar     一个简洁的avaScript日历控件,可在Java Web项目,.NET Web 项目中使用 二.kimsoft-jscalendar 有什么 ...

  3. Windows下通过脚本快速修改IP地址

    Windows下通过脚本快速修改IP地址 如果通过Windows的网络属性修改Ip/网关,真是太麻烦了. 经常要切换ip,所以我写了两个脚本: c:\办公室.bat netsh interface i ...

  4. JSPatch 动态更新,bug修复

    本文贴出项目中热修复的代码片段: require('UIView, JPObject, HtmlAllViewController,DataManager,EMClient,EaseMessageVi ...

  5. 素数回文(dfs,有bug)

    素数回文 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  6. javascript之函数节流

    对于高频率的事件触发,为了优化页面性能,我们一般会对其做函数节流.比如: resize.keydow.scroll事件等.用户的频繁操作,会导致事件高频率的执行,这样会出现页面抖动啊.频繁调接口啊等问 ...

  7. sublime安装和汉化

    对程序员来说,在不同的平台下有不同的IDE,网上很多教程都是使用DW,以致DW大名鼎鼎.其实,还有一些我们不为熟知的,却超级牛X的编辑器,我相信Sublime Text就是其中之一. 官方下载地址:h ...

  8. javascript模式——Mixin

    Mixin是一种扩展收集功能的方式,能提高代码的复用率. 在javascript中,原型可以继承于其它对象的原型,并且可以为任意数量的实例定义属性.可以利用这一点来促进函数的复用. 下面一段代码就是将 ...

  9. offset

    document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.docume ...

  10. 基于zepto的手机焦点图touchstart touchmove

    基于zepto的手机焦点图,查看地址:demo (建议使用手机浏览器查看)代码如下: <!DOCTYPE HTML> <html> <head> <title ...