【LeetCode】4Sum 解题报告
【题目】
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)
【解析】
3Sum和 3Sum 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 解题报告的更多相关文章
- LeetCode: Permutations 解题报告
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- leetcode—Palindrome 解题报告
1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...
- LeetCode C++ 解题报告
自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- LeetCode: Subsets 解题报告
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- LeetCode: isSameTree1 解题报告
isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary t ...
- LeetCode: Combinations 解题报告
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
- LeetCode: solveSudoku 解题报告
Sudoku SolverWrite a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are in ...
随机推荐
- C语言的本质(20)——预处理之二:条件预处理和包含头文件
我们可以通过定义不同的宏来决定编译程序对哪些代码进行处理.条件编译指令将决定那些代码被编译,而哪些是不被编译的.可以根据表达式的值或者某个特定的宏是否被定义来确定编译条件. 条件编译可分为三种情况,按 ...
- 推荐一款JavaScript日历控件:kimsoft-jscalendar
一.什么是 kimsoft-jscalendar 一个简洁的avaScript日历控件,可在Java Web项目,.NET Web 项目中使用 二.kimsoft-jscalendar 有什么 ...
- Windows下通过脚本快速修改IP地址
Windows下通过脚本快速修改IP地址 如果通过Windows的网络属性修改Ip/网关,真是太麻烦了. 经常要切换ip,所以我写了两个脚本: c:\办公室.bat netsh interface i ...
- JSPatch 动态更新,bug修复
本文贴出项目中热修复的代码片段: require('UIView, JPObject, HtmlAllViewController,DataManager,EMClient,EaseMessageVi ...
- 素数回文(dfs,有bug)
素数回文 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- javascript之函数节流
对于高频率的事件触发,为了优化页面性能,我们一般会对其做函数节流.比如: resize.keydow.scroll事件等.用户的频繁操作,会导致事件高频率的执行,这样会出现页面抖动啊.频繁调接口啊等问 ...
- sublime安装和汉化
对程序员来说,在不同的平台下有不同的IDE,网上很多教程都是使用DW,以致DW大名鼎鼎.其实,还有一些我们不为熟知的,却超级牛X的编辑器,我相信Sublime Text就是其中之一. 官方下载地址:h ...
- javascript模式——Mixin
Mixin是一种扩展收集功能的方式,能提高代码的复用率. 在javascript中,原型可以继承于其它对象的原型,并且可以为任意数量的实例定义属性.可以利用这一点来促进函数的复用. 下面一段代码就是将 ...
- offset
document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.docume ...
- 基于zepto的手机焦点图touchstart touchmove
基于zepto的手机焦点图,查看地址:demo (建议使用手机浏览器查看)代码如下: <!DOCTYPE HTML> <html> <head> <title ...