LeetCode进阶之路(4Sum)
2016-07-19 00:08
155 查看
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: The solution set must not contain duplicate quadruplets.
题目:和2sum、3sum类似,求4个的和。所以可以直接套用3sum的算法,但是这种解法超过时间限制。有点晚了,先贴上代码,明天有空研究。下面的两个方法还是直接用的3Sum,没做修改。
7.19更新:重新修改3sum、2sum算法,整理了一下逻辑。
public class Solution {
List<List<Integer>> list2 = new ArrayList<List<Integer>>();
public List<List<Integer>> fourSum(int[] nums, int target) {
if(nums == null || nums.length < 4) return list2;
Arrays.sort(nums);
for(int i = 0; i < nums.length - 3;i++) {
if(i > 0&& nums[i] == nums[i-1])continue;
int[] num = Arrays.copyOfRange(nums, i+1, nums.length);
threeSum(num, target-nums[i], nums[i]);
}
return list2;
}
public void threeSum(int[] nums, int target, int startNum){
if(nums == null || nums.length < 3) return ;
Arrays.sort(nums);
for(int i = 0;i < nums.length-2;i++) {
if(i > 0 && nums[i] == nums[i-1])continue;
int[] num = Arrays.copyOfRange(nums, i+1, nums.length);
twoSum(num, target-nums[i], nums[i], startNum);
}
}
public void twoSum(int[] nums, int target, int n, int startNum) {
int left = 0;
int right = nums.length -1;
while(left < right) {
if(left> 0 && nums[left-1] == nums[left]){
left++;
continue;
}
if(right < nums.length -1 && nums[right + 1] == nums[right]){
right--;
continue;
}
if(nums[left] + nums[right] == target) {
List<Integer> l = new ArrayList<Integer>();
l.add(nums[left]);
l.add(nums[right]);
l.add(n);
l.add(startNum);
list2.add(l);
left++;
right--;
}else if(nums[left] + nums[right] < target) {
left++;
}else {
right--;
}
}
}
}
每天下班回来都快10点了,洗澡弄完都11点多,有时候累的真想直接躺下睡觉,但是一想,现在不努力,以后还会有机会吗?咬咬牙还是把这个任务完成了再睡,至少熟悉下题目,思考一会。
target? Find all unique quadruplets in the array which gives the sum of target.
Note: 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] ]
题目:和2sum、3sum类似,求4个的和。所以可以直接套用3sum的算法,但是这种解法超过时间限制。有点晚了,先贴上代码,明天有空研究。下面的两个方法还是直接用的3Sum,没做修改。
7.19更新:重新修改3sum、2sum算法,整理了一下逻辑。
public class Solution {
List<List<Integer>> list2 = new ArrayList<List<Integer>>();
public List<List<Integer>> fourSum(int[] nums, int target) {
if(nums == null || nums.length < 4) return list2;
Arrays.sort(nums);
for(int i = 0; i < nums.length - 3;i++) {
if(i > 0&& nums[i] == nums[i-1])continue;
int[] num = Arrays.copyOfRange(nums, i+1, nums.length);
threeSum(num, target-nums[i], nums[i]);
}
return list2;
}
public void threeSum(int[] nums, int target, int startNum){
if(nums == null || nums.length < 3) return ;
Arrays.sort(nums);
for(int i = 0;i < nums.length-2;i++) {
if(i > 0 && nums[i] == nums[i-1])continue;
int[] num = Arrays.copyOfRange(nums, i+1, nums.length);
twoSum(num, target-nums[i], nums[i], startNum);
}
}
public void twoSum(int[] nums, int target, int n, int startNum) {
int left = 0;
int right = nums.length -1;
while(left < right) {
if(left> 0 && nums[left-1] == nums[left]){
left++;
continue;
}
if(right < nums.length -1 && nums[right + 1] == nums[right]){
right--;
continue;
}
if(nums[left] + nums[right] == target) {
List<Integer> l = new ArrayList<Integer>();
l.add(nums[left]);
l.add(nums[right]);
l.add(n);
l.add(startNum);
list2.add(l);
left++;
right--;
}else if(nums[left] + nums[right] < target) {
left++;
}else {
right--;
}
}
}
}
每天下班回来都快10点了,洗澡弄完都11点多,有时候累的真想直接躺下睡觉,但是一想,现在不努力,以后还会有机会吗?咬咬牙还是把这个任务完成了再睡,至少熟悉下题目,思考一会。
种一棵树最好的时间是十年前,其次是现在!
相关文章推荐
- js 基础 ---实现放大镜的效果
- Java中的UDP编程
- java---验证码技术演示网页显示(XML 网页 javaservlet)
- HDU题目分类
- BZOJ2588: Spoj 10628. Count on a tree
- Unity3d 刚体
- mysql left join 左连接查询关联n多张表
- 第24日:实施质量保证 和 组建项目团队
- 第24日:实施质量保证 和 组建项目团队
- 野指针与野指针的避免
- ruby爬虫日记1
- Hardware Acceleration(硬件加速)
- 异常
- UE4利用Save Game创建全局变量
- 表情包36年出位史
- 如何在 Objective-C 的环境下实现 defer
- 逐层拼接云原生栈
- python线程、进程和协程详解
- python异常和文件处理机制详解
- python对象及面向对象技术详解