【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 ...
随机推荐
- ZigBee 协议规范
ZigBee协议栈体系结构由应用层.应用汇聚层. 网络层.数据链路层和物理层组成,如下图所示: 图1 ZigBee 协议栈体系的层次结构 应用层定义了各种类型的应用业务,是协议栈的最 ...
- Response JSON数据返回
简述: 在servlet填充Response的时候,做JSON格式的数据转换 使用的类是net.sf.json.JSONObject,传入response对象和返回的显示类,修改response,返回 ...
- C Deepin指针
L-value -->标识了一个可以存储结果值的内存存储地址--指针变量 or 普通内存变量; R-value -->数值;
- 高龄“潮男”优衣库老板柳井正_榜样_奢华主义_YOKA时尚网
高龄"潮男"优衣库老板柳井正_榜样_奢华主义_YOKA时尚网 高龄"潮男"优衣库老板柳井正
- CSS 定位 (Positioning)学习
最近被css的定位要搞疯了...下面我总结一下最近学习东西. 先介绍几个概念: 块框:div.h1 或 p 元素常常被称为块级元素.这意味着这些元素显示为一块内容,即“块框”. 行内框:与之相反,sp ...
- ps 导出png-8图片会变模糊
出现这种情况的时候,在保存png-8图片的时候,索引色需要改为256,否则保存的图片颜色与原图会不一致.
- 【MFC学习笔记-作业7-小型画图软件】【】
作业要求: 按下鼠标右键画圆. 按下鼠标左键移动曲线. 丝毫没有思路..网上教程又比这个程序复杂100倍... 好吧 总算找到一个合适的了... 转载至:http://blog.chinaunix.n ...
- MVC3学习随记一
最近才接触mvc,也是才接触linq语法,还有EntiyFramework,个人感觉这种开发模式还是挺不错的,随手记点笔记,简单做个增删改查吧 一.实例化上下文ObjectContext: 引用空间那 ...
- 详解 CSS 属性 - 伪类和伪元素的区别(再也不用概念盲了!!!)
首先,阅读 w3c 对两者的定义: CSS 伪类用于向某些选择器添加特殊的效果. CSS 伪元素用于将特殊的效果添加到某些选择器. 可以明确两点,第一两者都与选择器相关,第二就是添加一些“特殊”的效果 ...
- 取得网站的IP 地址
select utl_inaddr.get_host_address('smtp.163.com') ipaddress from dual;