Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution {
public int threeSumClosest(int[] nums, int target) {
if(nums.length<3) return 0; int sum;
int ret=nums[0]+nums[1]+nums[2]; //initialize return value
int len = nums.length-2;
int left; //point to the left side of the array
int right; //point to the right side of the array Arrays.sort(nums); for(int i = 0; i < len; i++){
left = i+1;
right = len+1; while(left < right){
sum = nums[i] + nums[left] + nums[right];
if(sum > target){
right--;
}
else if(sum < target){
left++;
}
else{
return target;
} if(Math.abs(target - sum) < Math.abs(target - ret)) ret = sum;
} //skip repeated digital
while(nums[i] == nums[i+1]) {
if(i+1 >= len) break;
i++;
}
}
return ret;
}
}

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

  1. leetcode 16. 3Sum Closest JAVA

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

  2. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  3. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  4. 《LeetBook》leetcode题解(16):3Sum Closest [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

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

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

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

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

  8. 15. 3Sum、16. 3Sum Closest和18. 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

  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. python3 模拟鼠标和键盘操作

    1. 安装pyperclip pip install pyperclip 使用方法复制 pyperclip.copy("hello world") 粘贴 pyperclip.pas ...

  2. Centos7下GlusterFS 分布式文件系统环境搭建

    Centos7下 GlusterFS 环境搭建准备工作glusterfs-3.6.9.tar.gzuserspace-rcu-master.zip三台服务器:192.168.133.53.192.16 ...

  3. Linux 版 SecureCRT 界面变为 Windows 2000 风格的解决办法

    SecureCRT 是一款非常好用的远程终端连接软件,支持 Windows.Linux.macOS 全平台.由于现在工作平台主要在 Linux 系统上,SecureCRT 也是必备软件.一开始安装的是 ...

  4. 关于eclipse创建web工程没有生成webapp文件夹的解决方案

    先看工程建立的是不是配置的打成War包,然后按下图所示

  5. android.support.v4.app.NotificationCompat引用包

    在导入使用了ViewPage,ActionBar,Fragment的工程后出现错误,很有可能是没有导入4.0版本的支持包.本人也是碰到这个问题,特意搜索了一下,得到解决办法如下,记录下来,以免忘记.  ...

  6. eclipse修改android项目的apk包名类名

    在Google提供的Eclipse集成开发环境adt-bundle下修改名称的总结: 1.      修改工程名(apk名称) 在弹出的对话框中输入新名称 该操作实际上是修改<project&g ...

  7. postgresql清理工具

    1. 每个DB都单独进行了vacuumdb的命令: vacuumdb -d mydb -z -v 2. full vacuum : vacuumdb -a  -f -z -v  .  自动vacuum ...

  8. python 二叉树实现

    二叉树实现思想 1.把每个节点都看作是一个对象包含以下特征: 节点的当前值 节点的左孩子(存储比当前节点值小的节点对象) 节点右孩子(存储比当前节点值大的节点对象) 2.二叉树就是以根节点开始的连续的 ...

  9. Vue stage2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. ReactNative 学习笔记

    1. react-native引入第三方库时报Command `run-android` unrecognized: 在使用第三方库tab-navigator时调用: npm install reac ...