4 sum
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.
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]
]
从数组中找出四个字之和为target的。因为有多组,所以要从头遍历到尾。
找出四个数之和为target,做法和三个数之和一样,就是外面多套一层循环。也就是 先排序,遍历数组,然后从剩下的数组中找出三个元素与当前元素之和为target,三个元素之和又是遍历剩下的数组,再从剩剩下的数组中找出两个数,使他们与前两个遍历的元素的和为target,这两个元素从两头往中间遍历。期间要跳过重复元素。具体见代码。每次遍历时还可以多加判断条件,如当前元素和最后三个元素 的和小于target,这时就进入下轮循环了,要使左边数字增大才行。
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> result=new ArrayList<List<Integer>>();
for(int i=0;i<nums.length-3;i++){
if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;//前面四个都已经大于target了,就说明没有了(有序)
//当前数字和最后三个数字的和小于target,则将当前数字后移,也就是进行下一个循环
if(nums[i]+nums[nums.length-1]+nums[nums.length-2]+nums[nums.length-3]<target) continue;
if(i==0||(i>0&&nums[i]!=nums[i-1])){
for(int j=i+1;j<nums.length-2;j++){
if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target) break;
if(nums[i]+nums[j]+nums[nums.length-1]+nums[nums.length-2]<target) continue;
if(j==i+1||(j>i+1&&nums[j]!=nums[j-1])){
int left=j+1,right=nums.length-1,sum=target-nums[i]-nums[j];
while(left<right){
if(nums[left]+nums[right]==sum){
result.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
while(left<right&&nums[left]==nums[left+1]) left++;
while(left<right&&nums[right]==nums[right-1]) right--;
left++;right--;
}else if(nums[left]+nums[right]>sum) {
right--;
}else{
left++;
}
}
}
}
}
}
return result;
}
}
4 sum的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- Android Paint类介绍以及浮雕和阴影效果的设置
Paint类介绍 Paint即画笔,在绘制文本和图形用它来设置图形颜色, 样式等绘制信息. 1.图形绘制 setARGB(int a,int r,int g,int b); 设置绘制的颜色,a代表透明 ...
- 详解EBS接口开发之WIP模块接口
总体说明 文档目的 本文档针对WIP模块业务功能和接口进行分析和研究,对采用并发请求方式和调用API方式分别进行介绍 内容 WIP模块常用标准表简介 WIP事物处理组成 WIP相关业务流程 WIP相关 ...
- iOS积分抽奖Demo,可以人为控制不同奖项的得奖率
最近公司让写一个转盘积分抽奖的样式,所以把创建过程中的心得记录一下,给大家分享 首先创建了相关的图片转盘,指针图片,然后就是考虑转盘如何旋转的问题,我是通过给指针图片添加一个动画效果,从而实现旋转效果 ...
- AnimatedPathView实现自定义图片标签
老早用过小红书app,对于他们客户端笔记这块的设计非常喜欢,恰好去年在小红书的竞争对手公司,公司基于产品的考虑和产品的发展,也需要将app社交化,于是在社区分享这块多多少少参照了小红书的设计,这里面就 ...
- UNIX环境高级编程——主线程与子线程的退出关系
我们在一个线程中经常会创建另外的新线程,如果主线程退出,会不会影响它所创建的新线程呢?下面就来讨论一下. 1. 主线程等待新线程先结束退出,主线程后退出.正常执行. 示例代码: #include & ...
- TCP的ACK确认系列 — 快速确认
主要内容:TCP的快速确认.TCP_QUICKACK选项的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 快速确认模式 (1) 进入快速确认模式 ...
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十一)
机器人发射子弹已经完成了,下面看看怎么给玩家设置障碍. 大家从上篇的图可以看到,在机器和篮筐直接有若干障碍物.我们先看如何实现它们. 打开SpriteBuilder,在Sprites文件夹中新建Sma ...
- nginx 平滑升级
怎么能在不停止服务的情况下,平滑的升级nginx?下面告诉你答案,其实很简单 1.下载nginx新版本,并解压,进入解压的目录 2.你要执行旧版本的nginx -V来查看旧版本编译的时候,编译了什么模 ...
- C#之流程控制语句
通过一系列的学习,我们知道尽管计算机可以完成工作,但实质上这些工作都是按照我们事先编好的程序执行的,所以,程序是计算机的灵魂,计算机程序执行的控制流程由三种基本的控制结构控制,即顺序结构,选择结构,循 ...
- saiku运行时报错max_length_for_sort_data 需要set higher
infiniDB或者mysql数据库,运行时,按某个字段排序会出错.报错:max_length_for_sort_data ... set higher. saiku报错, 也是这样. 这是数 ...