题目: https://leetcode.com/problems/3sum-closest/

【标签】Array; Two Pointers

【个人分析】

  这道题和它的姊妹题 3Sum 非常类似, 就不再多说了,具体一些的分析可以参考 [Leetcode][015] 3Sum

 public class Solution {
public int threeSumClosest(int[] nums, int target) {
int result = target;
int len = nums.length;
if (len < 3) {
return 0;
}
Arrays.sort(nums);
for (int i = 0; i < len; i++) {
int number = nums[i]; int leftIndex = i + 1;
int rightIndex = len - 1;
while (leftIndex < rightIndex) {
int threeSum = number + nums[leftIndex] + nums[rightIndex];
if (threeSum == target) {
// best result found!
return target;
} else {
// update global result
if ( result == target ||
Math.abs(target - threeSum) < Math.abs(target - result)) {
result = threeSum;
}
if (threeSum < target) {
while (leftIndex < rightIndex &&
nums[leftIndex] == nums[leftIndex + 1]) {
leftIndex++;
}
leftIndex++;
} else {
while (leftIndex < rightIndex &&
nums[rightIndex] == nums[rightIndex - 1]) {
rightIndex--;
}
rightIndex--;
}
}
}
}
return result;
} }

[Leetcode][016] 3Sum Closest (Java)的更多相关文章

  1. 【JAVA、C++】LeetCode 016 3Sum Closest

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

  2. leetcode 16. 3Sum Closest JAVA

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

  3. [Leetcode]016. 3Sum Closest

    public class Solution { public int threeSumClosest(int[] num, int target) { int result = num[0] + nu ...

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

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

  5. No.016 3Sum Closest

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

  6. 【leetcode】3Sum Closest

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

  7. LeetCode--No.016 3Sum Closest

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

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

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

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

随机推荐

  1. 2016年9月3日 文成小盆友python-num18 - django进阶一

    一.深入django的路由系统 下面为django的请求生命周期 下面来看下整个生命周期中的路由系统: 在Django的urls中我们可以根据一个URL对应一个函数名来定义路由规则如下: " ...

  2. IO通信

    DeviceIoControl 发送控制代码到指定设备驱动程序 参数解释: hDevice Long,设备句柄 dwIoControlCode Long,应用程序调用驱动程序的控制命令,就是IOCTL ...

  3. 网站注册信息的JS全码

    <div class="box_index2">                <div class="login_title">    ...

  4. Tuple初探

    /*****************版权声明************************/ 大家好,我是小鸭,有问题可来邮件咨询:lxyjiang@gmail.com /************* ...

  5. 获取MP3和M4A音乐文件的歌曲信息以及专辑图片--备用

    NSBundle* bundle = [NSBundle mainBundle];     NSString* path = [bundle bundlePath];     NSURL * file ...

  6. Android 自定义控件 EditText输入框两边加减按钮Button

    自己封装的一个控件:EditText两边放加减按钮Button来控制输入框的数值 Demo 下载地址: 第一版:http://download.csdn.net/detail/zjjne/674086 ...

  7. 自动删除n天前日志

    自动删除n天前日志 linux 是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,让系统定时清理一些不需要的文件很有一种 爽快的事情.不用你去每 ...

  8. COJ 0801 非传统题(一)

    非传统题(一) 难度级别:A: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 大家好!我是COJ第一道非传统题,是不是感觉非常的excite ...

  9. BZOJ3433: [Usaco2014 Jan]Recording the Moolympics

    3433: [Usaco2014 Jan]Recording the Moolympics Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 55  So ...

  10. 如何消除word中的回车符号

    打开文字编辑页面,在菜单栏上选择工具-选项-视图-格式标志中的“段落标志”复选框前面的“√”去掉即可.  附件: