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 PakageManagerService启动流程分析
PakageManagerService的启动流程图 1.PakageManagerService概述 PakageManagerService是android系统中一个核心的服务,它负责系统中Pac ...
- JavaScript中的三种弹出对话框
学习过js的小伙伴会发现,我们在一些实例中用到了alert()方法.prompt()方法.prompt()方法,他们都是在屏幕上弹出一个对话框,并且在上面显示括号内的内容,使用这种方法使得页面的交互性 ...
- 分布式数据库HBase表设计
比较常用的数据库是关系型数据库,但很多场景下nosql数据库会更加擅长,从sql到nosql实施的第一步就是设计表结构,这是两种不同的思维方式,这里说下HBase表设计. 需求:需要一张stock表用 ...
- (六十八)使用XMPPFramework登录
按照XMPPFramework的官方样例,应该把登录代码放置在AppDelegate中,并且让注销成为私有方法. XMPPFramework进行登录的步骤如下: ①连接主机,并且发送JID ②如果连接 ...
- Mybatis事务(二)事务隔离级别
一般数据库的隔离级别有4个,由低到高依次为Read uncommitted.Read committed.Repeatable read.Serializable,这四个级别可以逐个解决脏读.不可重复 ...
- 【翻译】如何在Ext JS 6中使用Fashion美化应用程序
原文:How to Style Apps with Fashion in Ext JS 6 在Ext JS 6,一个最大的改变就是框架合并,使用一个单一的代码库,就可以为每一种设备开发各具有良好体验的 ...
- ARM v7汇编与相关练习
程序入口: _startc 语言入口: main@: 注释;main: 标签;伪指令: 给汇编器读的指令;.global main ...
- Linux管理日记(二)
14 启动内网端口转发 ### 2. 查看IP以及网卡信息 # 可以发现, eth0 的地址 inet addr:10.144.7.195, 此为内网网卡 # 公网网卡, eth1 的地址 inet ...
- java工具类(三)之生成若干位随机数
java 生成若干位随机数的问题 在一次编程的过程中偶然碰到一个小问题,就是需要生成一个4位数的随机数,如果是一个不到4位大的数字,前面可以加0来显示.因为要求最后是一个4位的整数,不带小数点.当时就 ...
- java垃圾回收机制,以及常用的回收算法
记得之前去平安面试的时候,面试官问到了垃圾回收,我当时也就是说说了垃圾回收的原理,但是具体有哪些实现策略,我当时是懵的. 概念: Java的垃圾回收机制是Java虚拟机提供的能力,用于在空闲时间以不定 ...