【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 ...
随机推荐
- Delphi通过GetFileVersionInfo和VerQueryValue等API函数取得详细EXE信息
This has been described at About: http://delphi.about.com/cs/adptips2001/a/bltip0701_4.htmBasically, ...
- linux之SQL语句简明教程---UPDATE DELETE FROM
我们有时候可能会需要修改表格中的资料.在这个时候,我们就需要用到 UPDATE 指令.这个指令的语法是: UPDATE "表格名" SET "栏位1" = [新 ...
- Stackoverflow上人气最旺的10个Java问题(转ImportNew)
本文转自:http://www.importnew.com/16841.html 写的确实太好了 1.为什么两个(1927年)时间相减得到一个奇怪的结果? 如果执行下面的程序,程序解析两个间隔1秒的日 ...
- java中如何将char数组转化为String
1.直接在构造String时建立. char data[] = {'s', 'g', 'k'}; String str = new String(data); 2.String有方法可以直接转换. S ...
- 关于linux系统下的uname -a命令
- 剑指offer57 删除链表中重复的结点
class Solution { public: ListNode* deleteDuplication(ListNode* pHead) { if(!pHead) return pHead; str ...
- gmapping 学习
为解决斜坡下gmapping定位的问题,开始关注gmapping. 先看到EAIPOT博客里关于gmapping的一个参数文件 <launch> <arg name="sc ...
- cf公式专场-续
Benches Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- windows7下virtualBox配置识别usb
在windows7下安装virtualBox后.在虚拟机里面是不能识别手机的,此时我们须要做一些配置. 一. virtualBox菜单: 管理–全局设定–扩展–加入包(右側,virtualBox ex ...
- 链接脚本之LMA VMA解释
链接脚本中的LMA和VMA是什么意思.这个问题纠结了一段时间,今天在看<ARM体系结构与编程>时,豁然开朗,写下自己的认识.分享例如以下: LMA:载入地址 位于存储器中的地址 LOAD ...