题目:

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

解题思路:

将nums数组重新排列,然后从i = 0开始遍历,分别取left = i + 1和right = nums.length - 1,将nums[i]、nums[left]和nums[right]三数之和与target差值的绝对值与diff比较,如果小于diff,则使sum赋值为此三数之和,并且将diff的值修改为三数之和与target差值的绝对值

class Solution {
public int threeSumClosest(int[] nums, int target) {
int diff = Integer.MAX_VALUE, result = 0;
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1, right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == target) {
return sum;
}
if (diff > Math.abs(target - sum)) {
diff = Math.abs(target - sum);
result = sum;
}
if (sum > target) { //因为sum > target,因此只需将right向左调整,此时sum的值则会减少,这样才能找到更接近的target的三数之和
right--;
} else {
left++; //因为sum <= target,因此只需将left向右调整,此时sum的值则会增加,这样才能找到更接近target的三数之和
}
}
}
return result;
}
}

leetcode 16. 3Sum Closest JAVA的更多相关文章

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

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

  2. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  3. [Leetcode][016] 3Sum Closest (Java)

    题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...

  4. Java [leetcode 16] 3Sum Closest

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

  5. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

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

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

  7. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

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

  8. Leetcode 16. 3Sum Closest

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

  9. [LeetCode] 16. 3Sum Closest 解题思路

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

随机推荐

  1. django-rest-swagger

    前提工作 pip3 install --user django>=2.0.0 pip3 install --user django-rest-swagger 安装完成之后,创建一个django项 ...

  2. 用django框架开发一个B2C购物网站用户注册知识点总结2

    一:用户部分: 用户注册: 用户注册序列化器: import re from django_redis import get_redis_connection from rest_framework ...

  3. go_数组

    go语言中数组是值类型 [10]int 和 [20]int 是不同类型,不能用作参数传递 调用func f(arr [10]int)会拷贝数组 go语言一般不用数组用切片slice package m ...

  4. emulator: Trying to vcpu execute at eip:6d4053

  5. 日志管理,springboot

    1.市面上的日志框架:JUL.JCL.Jboss-logging.logback.log4j.log4j2.slf4j.... 2. 日志门面: SLF4J:日志实现:Logback:SpringBo ...

  6. StringBuffer详解

  7. pthread_mutex_init函数与pthread_mutexattr_init函数

    直接上英文解释: pthread_mutex_init()如下: NAME pthread_mutex_init, pthread_mutex_destroy - initialise or dest ...

  8. Windows下redis的安装与使用

    Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  9. [SoapUI] 获取Cookie,并循环遍历当前Project下所有的Test Suite,Test Case,Test Step,将Cookie传递给这些Test Step

    import com.eviware.soapui.support.types.StringToStringMap //Get all th cookies in the response , her ...

  10. 向对象(OO)程序设计

    http://www.uml.org.cn/mxdx/201208232.asp 前言 本文主要介绍面向对象(OO)程序设计,以维基百科的解释: 面向对象程序设计(英语:Object-oriented ...