3 Sum

Given an array S of n integers, are there elements abc in Ssuch that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Notice

Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)

The solution set must not contain duplicate triplets.

Example

For example, given array S = {-1 0 1 2 -1 -4}, A solution set is:

(-1, 0, 1)
(-1, -1, 2)

分析:

这题还是很简单的,使用3个指针即可解决问题。关键是里面重复数字的处理需要当心。
 public class Solution {
/**
* @param numbers : Give an array numbers of n integer
* @return : Find all unique triplets in the array which gives the sum of zero.
*/
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>>();
if(num == null || num.length < ) {
return rst;
}
Arrays.sort(num);
for (int i = ; i < num.length - ; i++) {
13 if (i != 0 && num[i] == num[i - 1]) {
14 continue; // to skip duplicate numbers; e.g [0,0,0,0]
15 } int left = i + ;
int right = num.length - ;
while (left < right) {
int sum = num[left] + num[right] + num[i];
if (sum == ) {
ArrayList<Integer> tmp = new ArrayList<Integer>();
tmp.add(num[i]);
tmp.add(num[left]);
tmp.add(num[right]);
rst.add(tmp);
left++;
right--;
while (left < right && num[left] == num[left - 1]) { // to skip duplicates
30 left++;
31 }
32 while (left < right && num[right] == num[right + 1]) { // to skip duplicates
33 right--;
34 }
} else if (sum < ) {
left++;
} else {
right--;
}
}
}
return rst;
}
}

4Sum

Given an array S of n integers, are there elements abc, andd in S such that a + b + c + d = target?

Find all unique quadruplets in the array which gives the sum of target.

Notice

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.

 
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)
分析:原理同上

 public class Solution {
/**
* @param numbers : Give an array numbersbers of n integer
* @param target : you need to find four elements that's sum of target
* @return : Find all unique quadruplets in the array which gives the sum of
* zero.
*/
public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>>();
if(num == null || num.length < ) {
return rst;
}
Arrays.sort(num);
for (int i = ; i <= num.length - ; i++) {
if (i != && num[i] == num[i - ]) {
continue; // to skip duplicate numbers; e.g [0,0,0,0]
}
for (int j = i + ; j <= num.length - ; j++) {
if (j != i + && num[j] == num[j - ]) {
continue; // to skip duplicate numbers; e.g [0,0,0,0]
}
int left = j + ;
int right = num.length - ;
while (left < right) {
int sum = num[left] + num[right] + num[i] + num[j] - target;
if (sum == ) {
ArrayList<Integer> tmp = new ArrayList<Integer>();
tmp.add(num[i]);
tmp.add(num[j]);
tmp.add(num[left]);
tmp.add(num[right]);
rst.add(tmp);
left++;
right--;
while (left < right && num[left] == num[left - ]) { // to skip duplicates
left++;
}
while (left < right && num[right] == num[right + ]) { // to skip duplicates
right--;
}
} else if (sum < ) {
left++;
} else {
right--;
}
}
} }
return rst;
}
}

转载请注明出处: cnblogs.com/beiyeqingteng/

3Sum & 4Sum的更多相关文章

  1. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  2. LeetCode Two Sum&Two Sum II - Input array is sorted&3Sum&4Sum 一锅煮题解

    文章目录 Two Sum Two Sum II 3Sum 4Sum Two Sum 题意 给定一个数组,和指定一个目标和.从数组中选择两个数满足和为目标和.保证有且只有一个解.每个元素只可以用一次. ...

  3. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  4. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列

    1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...

  5. LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结

    前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...

  6. 3Sum,4Sum问题

    //三数和为0的问题.要求去重,并且输出数字有序.public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(n ...

  7. 秒杀 2Sum 3Sum 4Sum 算法题

    2 Sum 这题是 Leetcode 的第一题,相信大部分小伙伴都听过的吧. 作为一道标着 Easy 难度的题,它真的这么简单吗? 我在之前的刷题视频里说过,大家刷题一定要吃透一类题,为什么有的人题目 ...

  8. [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 = tar ...

  9. 算法题丨3Sum Closest

    描述 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

随机推荐

  1. java.io.stream

    1. package com.io.Stream; import java.io.*; public class NyFileInputStream1 { /** * 读取文件的streamIO * ...

  2. iOS开发中的错误整理,(百思项目,指示器位置)设置控件尺寸和点坐标,先设置尺寸,再设置点坐标

    之前对控件的尺寸和点的坐标的设置从来都是想到什么写什么,从来没有关心过顺序.然后就有了这次的血的教训!!!!! 下面是错误的截图,先设置的中心点,然后设置的宽度.程序运行就这样了,点别的没有毛病!!! ...

  3. IntellJ IDEA 所有快捷键

    登录下面网站. http://www.jetbrains.com/idea/documentation/ 下载Keymap for Windows/Linux 后面的PDF文档.

  4. CentOS 安装 Jexus

    官网:http://www.jexus.org/ 安装过程就照着页面上做就好了,前提是需要安装好mono 在VS2015中新建一个MVC应用程序,这里需要注意两个步骤: 第1步:移除bin下的Micr ...

  5. sql-select

    SELECT 将资料从数据库中的表格内选出 指令 SELECT "栏位名" FROM "表格名"; 例如:查询Store_Information表中所有的的St ...

  6. 详解HTML中的window对象和document对象

    Window -- 代表浏览器中一个打开的窗口: 对象属性 window //窗口自身 window.self //引用本窗户window=window.self window.name //为窗口命 ...

  7. 【HDU 2604】Queuing

    题 题意 f和m两种字母组成字符串,fmf 和 fff 这种为不安全的字符串,现在有2*L个字母,问你有多少安全的字符串.答案mod M. 分析 递推,这题本意是要用矩阵快速幂.不过我发现这题好神奇, ...

  8. android studio问题-ICCP:Not recognizing known sRGB profile

    转:http://my.oschina.net/1pei/blog/479162 PNG格式:每个PNG文件是由一个PNG标识(signature),后面跟一些数据块(chunk),每个chunk由 ...

  9. 车牌号对应归属地及城市JSON带简码

    车牌号对应归属地及城市JSON带简码 car_city.json [ { "code": "冀A", "city": "石家庄&q ...

  10. 类,抽象基类,接口类三者间的区别与联系(C++)

    结构上的区别: 普通类:数据+方法+实现 抽象类:数据+方法(一定包含虚方法n>=1)+部分方法的实现 接口类:方法(纯虚方法) http://www.cnblogs.com/Tris-wu/p ...