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. 在Eclipse 中安装插件 Activiti

    (1)在eclipse中菜单help->Install New software中,点击add (2)输入要安装的插件的名字和路径 Name:Activiti BPMN 2.0 designer ...

  2. Java算法-希尔排序

    希尔排序的诞生是由于插入排序在处理大规模数组的时候会遇到需要移动太多元素的问题.希尔排序的思想是将一个大的数组“分而治之”,划分为若干个小的数组,以 gap 来划分,比如数组 [1, 2, 3, 4, ...

  3. 中国天气网-天气预报接口api

    中国天气网地址:http://www.weather.com.cn 请求服务 : 查询实时天气信息 http://www.weather.com.cn/data/sk/101110101.html 在 ...

  4. MyEclipse------executeBatch()使用方法

    executeBatch()方法用于成批地执行SQL语句,但不能执行返回值是ResultSet结果集的SQL语句,而是直接执行stmt.executeBatch(); 辅助方法: addBatch() ...

  5. boost解析json

    #include <QtCore/QCoreApplication> #include <boost/property_tree/ptree.hpp> #include < ...

  6. 动态下载 Yahoo 网络数据存入 Microsoft SQL Server 再 Matlab 调用的一个完整例子

    % 编程环境: Matlab 2014a, win7 32bit, Microsoft SQL Server 2008r2 %% % 清屏 clc; clear all; close all; %% ...

  7. Unity Shader Billboard

    记录来源于ShaderLab开发实战详解 Shader "Tut/Project/Billboard_1" { Properties { _MainTex ("Base ...

  8. UrlConnection连接和Socket连接的区别

    关于UrlConnection连接和Socket连接的区别,只知道其中的原理如下: 抽象一点的说,Socket只是一个供上层调用的抽象接口,隐躲了传输层协议的细节. urlconnection 基于H ...

  9. ID

    id 编辑 身份标识号.账号.唯一编码.专属号码.工业设计.国家简称.法律词汇.通用账户.译码器.软件公司等,各类专有词汇缩写. 身份证,身份识别,是一种身份证明. 中文名 身份证,帐号,工业设计,通 ...

  10. 如何在word里面插入目录

    点击“引用”->插入目录