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. BACKBONE源代码解析

    //2014.11// Backbone.js 1.0.0 // (c) 2010-2013 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may b ...

  2. WAR包

    1.windows命令下使用cmd命令打包 jar -cvf applicationname.war package.*: 2.程序中使用代码打包(这里用java) try{ string strja ...

  3. COLORBOX文档

    1,flash覆盖colorbox: 2,colorbox在ie中的位置和行为异常: 3,colorbox的位置和行为异常(不区分浏览器): 4,用colorbox显示外部文档时显示不正确: 5,在i ...

  4. SpringMVC配置数据库连接池

    http://www.cnblogs.com/coqn/archive/2012/08/15/SpringMvc%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA%E9%85%8 ...

  5. SPOJ Pouring Water

    传送门 POUR1 - Pouring water #gcd #recursion Given two vessels, one of which can accommodate a litres o ...

  6. startx启动过程分析

    http://blog.csdn.net/hustwarhd/article/details/3069066 JiananHe 09/19/2008 目录 xinit 1.1      功能 1.2  ...

  7. iOS 知识点梳理

    OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装.继承.多态.它既具有静态语言的特性(如C++),又有动态语言的效率(动态绑定.动态加载等).总体来讲,OC确实是一门不错 ...

  8. tmux 快捷键

    ctrl+b , 修改窗口名称 ctrl+b ' 快速按名字切换窗口 ctrl+b w 列出窗口列表 Ctrl+b 激活控制台:此时以下按键生效 系统操作 ? 列出所有快捷键:按q返回 d 脱离当前会 ...

  9. boost库(条件变量)

    1相关理念 (1)类名 条件变量和互斥变量都是boost库中被封装的类. (2)条件变量 条件变量是thread库提供的一种等待线程同步的机制,可实现线程间的通信,它必须与互斥量配合使用,等待另一个线 ...

  10. Java Web文件上传

    参考资料:http://www.cnblogs.com/xdp-gacl/p/4200090.html 一.问题描述 Java Web文件上传需要借助一些第三方库,常用的是借助Apache的包,有两个 ...