给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.

与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).

这里和14是不同就是条件变为与target的距离最小,只要把15中==的条件变为本题所要求的的就好了,其他趋同。
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
//List<Integer> list = new List<Integer>();
int min_sum = Integer.MAX_VALUE,min_distance = Integer.MAX_VALUE;
for(int i = 0; i < nums.length - 2;i++){
int low = i + 1,high = nums.length - 1;
while(low < high){
int sum = nums[i] + nums[low] + nums[high];//这一句一定要放在while循环里面!!!!
if(Math.abs(sum - target) <min_distance){
min_sum = sum;
min_distance = Math.abs(sum - target);
}
if(sum > target){
high--;
}else{
low++;
}
} }
return min_sum;
}
}

2019-04-14 11:38:26

哎第二次写,第一下想到的还是这种思路(python)

 class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
n = len(nums)
nums.sort()
min_=sys.maxsize
for i in range (n-1):
L = i+1
R=n-1
while L < R:
temp = abs(nums[i]+nums[L]+nums[R]-target)
if temp < min_:
min_=temp
res=nums[i]+nums[L]+nums[R]
elif nums[i]+nums[L]+nums[R]>target:
R-=1
else:
L+=1
return res

2019-11-28 09:09:11

LeetCode--016--最接近的三数之和(java)的更多相关文章

  1. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  2. Java实现 LeetCode 16 最接近的三数之和

    16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...

  3. leetcode.数组.16最接近的三数之和-java

    1. 具体题目 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案 ...

  4. LeetCode 16. 最接近的三数之和(3Sum Closest)

    题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例 ...

  5. 【LeetCode】最接近的三数之和【排序,固定k1,二分寻找k2和k3】

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  6. [LeetCode] 16. 最接近的三数之和

    题目链接:https://leetcode-cn.com/problems/3sum-closest/ 题目描述: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 num ...

  7. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  8. Leetcode题库——16.最接近的三数之和

    @author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...

  9. LeetCode-016-最接近的三数之和

    最接近的三数之和 题目描述:给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只 ...

随机推荐

  1. Feign 重试解析

    Spring cloud Feign 在restful 调用失败后,会进行重试.在没有到达指定重试次数,会一直重试. @Override public Object invoke(Object[] a ...

  2. git迁移

    git迁移 项目开发的不同阶段可能要使用不同的git仓库,有时需要迁移. git有很好的方法,只需要几个命令 目标: 我们需要把代码从 http://a.com/projectA.git 迁移到 ht ...

  3. Django’s cache framework

    小结: 1.缓存存储位置:数据库.文件系统.内存 2.通过缓存前缀实现跨服务器缓存 Django’s cache framework | Django documentation | Django h ...

  4. ios安装ipa与安卓安装apk

    ideviceinstaller -i .ipa包所在的路径 环境搭建:Mac上安装brew(brew里面有很多命令,可以安装自己想用的命令) 安装命令如下:curl -LsSf http://git ...

  5. 软件加密工具-Virbox 开发者工具盒

    功能 Virbox 开发者工具盒是由深思数盾研发的一套软件加密工具,将加壳工具.API文档及操作流程文档等集成在一起,方便软件开发者使用. 您可以通过 Virbox 开发者工具盒实现: dll.exe ...

  6. Redis入门到高可用(十五)—— GEO

    一.简介 二.应用场景 三.API 1.geoadd 2.geopos 3.geodist 4.georadius 四.相关说明

  7. java框架之SpringBoot(4)-资源映射&thymeleaf

    资源映射 静态资源映射 查看 SpringMVC 的自动配置类,里面有一个配置静态资源映射的方法: @Override public void addResourceHandlers(Resource ...

  8. git pull 冲突拉取不到新的代码

    本地文件已经有冲突或者在pull的过程中拉取的文件和本地文件冲突时,拉取不到新的代码,git pull出现报错,如下: 这个时候,如果你有两种选择,如果你需要这些改动,那个你就需要手动解决冲突,然后a ...

  9. JDBC-DAO层数据访问工具类的实现

    private static PreparedStatement pst; private static ResultSet rst; public static <T> int inse ...

  10. Software Testing 3

    Questions: • 7. Use the following method printPrimes() for questions a–d. 基于Junit及Eclemma(jacoco)实现一 ...