题目描述:

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. You may assume that each input would have exactly one solution.

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

解题思路:

这个跟3Sum的思路差不多,不过实现的过程就简单多了。外面一个大的循环,里面也是从两边开始,如果三个数之和减去目标值的绝对值较小,则更新之并记录此三个数之和。

代码如下:

public class Solution {
public int threeSumClosest(int[] nums, int target) {
int length = nums.length;
int min = Integer.MAX_VALUE;
int left, right, comp, result = 0; if (length < 3)
return 0;
Arrays.sort(nums); for (int i = 0; i < length - 2; i++) {
left = i + 1;
right = length - 1;
while (left < right) {
comp = nums[i] + nums[left] + nums[right];
if (Math.abs(comp - target) < min){
min = Math.abs(comp - target);
result = comp;
}
if (comp > target)
right--;
else if (comp < target)
left++;
else
return target;
}
}
return result;
}
}

Java [leetcode 16] 3Sum Closest的更多相关文章

  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 16. 3Sum Closest JAVA

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

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

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

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

  6. 蜗牛慢慢爬 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 ...

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

  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

    一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...

随机推荐

  1. 1. Linux驱动开发之开篇--Makefile

    基本Makefile假设现在有3个文件,file2.h是函数声明,file2.c是函数定义,文件file1.c调用file2.c中的函数.则Makefile文件的编写如下: helloworld:fi ...

  2. opencv学习笔记(01)——操作图像的像素

    #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <ope ...

  3. cmd&Linux 下使用mysql全记录

    php mysql数据库常用cmd命令集 show databases; 显示数据库 create database name; 创建数据库 use databasename; 选择数据库 drop ...

  4. (转)Linux SLUB 分配器详解

    原文网址:https://www.ibm.com/developerworks/cn/linux/l-cn-slub/ 多年以来,Linux 内核使用一种称为 SLAB 的内核对象缓冲区分配器.但是, ...

  5. bnuoj 1057 函数(模拟)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=1057 [题意]:给定x的值,带入f(x)求函数值 [题解]:注意第一个数的符号可能是'+',这里把我 ...

  6. Linux操作系统工作的基础

    简介: 本文根据 Linux™ 系统工作基础的分析,对存储程序计算机.堆栈(函数调用堆栈)机制和中断机制进行概述.文中将为您提供操作系统(内核)如何工作的细节,进一步从宏观概述结合关键点进行微观(CS ...

  7. POJ - 1741 Tree

    DescriptionGive a tree with n vertices,each edge has a length(positive integer less than 1001).Defin ...

  8. Hibernate缓存机制简述 (转)

    感谢:http://blog.csdn.net/ramln1989/article/details/5528445 ------------------------------------------ ...

  9. Hibernate关系级别注解

    最近在学习Hibernate的相关知识,这一站学习的是Hibernate的注解相关的操作和知识.在这里标注以下为以后查阅和需要帮助的朋友提供便利. 一. 开发环境的搭建: 1. 需要的jar包配置: ...

  10. UNITY3D使用NGUI制作自适应UI的总结

    原地址:http://www.cnitblog.com/updraft/archive/2013/11/12/88801.html 制作自适应的几个方法1. 使用 UIROOT 里设置自定义高度的方法 ...