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

Notice

You may assume that each input would have exactly one solution.

Example

For example, given array S = [-1 2 1 -4], and target = 1. The sum that is closest to the target is 2(-1 + 2 + 1 = 2).

 public class Solution {
/**
* @param numbers: Give an array numbers of n integer
* @param target : An integer
* @return : return the sum of the three integers, the sum closest target.
*/
public int threeSumClosest(int[] num ,int target) {
int tempClosestSum = Integer.MAX_VALUE;
int diff = Integer.MAX_VALUE;
if(num == null || num.length < ) {
return tempClosestSum;
}
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]
} int left = i + ;
int right = num.length - ;
while (left < right) {
int sum = num[left] + num[right] + num[i];
if (sum - target == ) {
return target;
} else if (sum - target < ) {
if (Math.abs(sum - target) < Math.abs(tempClosestSum - target)) {
tempClosestSum = sum;
}
left++;
} else {
if (Math.abs(sum - target) < Math.abs(tempClosestSum - target)) {
tempClosestSum = sum;
}
right--;
}
}
}
return tempClosestSum;
}
}

3Sum Smaller

Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.

For example, given nums = [-2, 0, 1, 3], and target = 2.

Return 2. Because there are two triplets which sums are less than 2:

[-2, 0, 1]
[-2, 0, 3] 分析:https://segmentfault.com/a/1190000003794736
解题思路和3SUM一样,也是先对整个数组排序,然后一个外层循环确定第一个数,然后里面使用头尾指针left和right进行夹逼,得到三个数的和。如果这个和大于或者等于目标数,说明我们选的三个数有点大了,就把尾指针right向前一位(因为是排序的数组,所以向前肯定会变小)。
如果这个和小于目标数,那就有right - left个有效的结果。为什么呢?因为假设我们此时固定好外层的那个数,还有头指针left指向的数不变,那把尾指针向左移0位一直到左移到left之前一位,这些组合都是小于目标数的。
     public int threeSumSmaller(int[] nums, int target) {
// 先将数组排序
Arrays.sort(nums);
int cnt = ;
for (int i = ; i < nums.length - ; i++) {
int left = i + , right = nums.length - ;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
// 如果三个数的和大于等于目标数,那将尾指针向左移
if (sum >= target) {
right--;
// 如果三个数的和小于目标数,那将头指针向右移
} else {
// right - left个组合都是小于目标数的
cnt += right - left;
left++;
}
}
}
return cnt;
}

3Sum Closest & 3Sum Smaller的更多相关文章

  1. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  2. 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 ...

  3. No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  4. 【leetcode】3Sum Closest

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

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

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

  6. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  7. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

  8. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  9. LeetCode--No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

随机推荐

  1. Java Web中将oracle的数据库内容以表格形式展现到页面中(分页展示)

    分页SQL语句: ----分页显示 select * from (select rownum as r,t.* from () ; 查询的结果如下: 这个SQL,使用了三层嵌套的查询方式: 1)最内层 ...

  2. C#中async/await中的异常处理

    在同步编程中,一旦出现错误就会抛出异常,我们可以使用try-catch来捕捉异常,而未被捕获的异常则会不断向上传递,形成一个简单而统一的错误处理机制.不过对于异步编程来说,异常处理一直是件麻烦的事情, ...

  3. Tomcat+eclipse JSP windows开发环境配置

    一.安装Java SE http://www.oracle.com/technetwork/java/javase/downloads/index.html ,配置JAVA_HOME环境变量 二.安装 ...

  4. 常用的Java 架包(jar)的用途

    前言:如果需要在项目中引入jar包,可以采用maven,配置方式在 http://mvnrepository.com 查询  slf4j-api 简介:slf4j并不是一种具体的日志系统,而是一个用户 ...

  5. Java 缓存技术

    以下仅是对map对方式讨论.没有对点阵图阵讨论.作缓存要做以下2点:  1:清理及更新缓存时机的处理: . 虚拟机内存不足,清理缓存 .. 缓存时间超时,或访问次数超出, 启动线程更新 2:类和方法的 ...

  6. 遍历HashMap的四种方法

    public static void main(String[] args) { Map<String,String> map=new HashMap<String,String&g ...

  7. 【poj2546】 Circular Area

    http://poj.org/problem?id=2546 (题目链接) 题意 求两圆的面积交 Solution 一道水题Wa死我了,肯定是昨晚搞太晚的缘故= =. 两圆的位置关系有5种,而这里要求 ...

  8. NOIP 2012 T5 借教室 [洛谷P1083]

    题目描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要 向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自 ...

  9. Opencv加载和显示图片

    #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <ios ...

  10. c#中的23种设计模式

    C# 23种设计模式汇总 创建型模式 工厂方法(Factory Method) 在工厂方法模式中,工厂方法用来创建客户所需要的产品,同时还向客户隐藏了哪种具体产品类将被实例化这一细节.工厂方法模式的核 ...