题目:

给定一个包括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. 1.Hadoop集群搭建之Linux主机环境准备

    Hadoop集群搭建之Linux主机环境 创建虚拟机包含1个主节点master,2个从节点slave1,slave2 虚拟机网络连接模式为host-only(非虚拟机环境可跳过) 集群规划如下表: 主 ...

  2. 强大的vim配置文件,让编程更随意 (转载)

    花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim配置主要有以下优点: 1.按F5可以直接编译并执行C.C++.java代码以及执行shell脚本,按“F8”可进行C.C++代码的调试 2. ...

  3. C#给图片加文字水印

    public class TxtWaterMark { public enum WaterPositionMode { LeftTop,//左上 LeftBottom,//左下 RightTop,// ...

  4. iOS判断字母、数字串

    以下为NSString类的扩展方法,分别是判断字符串是否只是包含字母.是否只包含数字.是否只包含字母和数字: //字母 - (BOOL)cdm_isOnlyLetters { NSCharacterS ...

  5. 适配iOS10 调取系统打电话功能

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString: [NSString stringWithFormat:@"t ...

  6. ATM取款机的数据库模拟开发和实战总结

    一.ATM实战开发的简介. 学习了几天的Oracle,开始着手用数据库PL/SQL语言做一个简单的ATM取款机业务,主要是为了巩固数据库的知识,并非真正的去实现高端的业务.有兴趣的可以看看,希望对同胞 ...

  7. mac上,sudo启动IDEA

    cd /Applications/IntelliJ IDEA 14.app/Contents/MacOS sudo ./idea # 就是这个启动,我一开始没有找到这个启动项...这样你就能运行80端 ...

  8. [C++] NEW Advanced Usage

    NEW Advanced Usage 将分配的内存限定在特定的一块区域 #include<iostream> #include<new> ); ); }; using name ...

  9. noi2729 Blah数集

    Blah数集 大数学家高斯小时候偶然间发现一种有趣的自然数集合Blah,对于以a为基的集合Ba定义如下: (1) a是集合Ba的基,且a是Ba的第一个元素: (2)如果x在集合Ba中,则2x+1和3x ...

  10. sql2008 安装提示重启失败

    [转] https://www.cnblogs.com/chenshaogang/p/4313022.html