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. Android编程-Activity

    重要的回调函数: onCreate():初始化activity要用的其他的组件(可以是其他的activity和contentprovider之类).调用setContentView(),设定本Acti ...

  2. [UE4]Drop,扔物品

    一.Drop只要将Grab做的操作反着这一遍就可以:开启模拟物理.取消附加(使用:DetachFromActor方法,同样的rule也需要使用Keep World) 二.

  3. SAS 读取指定目录下文件列表宏

    OPTIONS PS=MAX LS=MAX NOCENTER SASMSTORE=SASUSER MSTORED MAUTOSOURCE;/*获取指定文件夹的指定类型的所有文件*/%MACRO GET ...

  4. 大数据学习之路(1)Hadoop生态体系结构

    Hadoop的核心是HDFS和MapReduce,hadoop2.0还包括YARN. Hadoop1.x的生态系统: Hadoop2.x引入YARN: HDFS(Hadoop分布式文件系统)源自于Go ...

  5. debian apache2 修改默认路径

    1.修改 /etc/apache2/sites-enable/000-default.conf 将DocumentRoot改成你的路径 2.修改 /etc/apache2/apache2.conf & ...

  6. iptables 配置说明

    #配置,禁止进,允许出,允许回环网卡 iptables -P INPUT DROP iptables -P OUTPUT ACCEPT iptables -A INPUT -i lo -j ACCEP ...

  7. 【转载】Google 程序员消灭 Bug 的 5 大法宝!

    遇到问题怎么办?还能怎么办,解决呗.那到底怎么解决呢?你是有什么惯用的逻辑模式.解决策略,还是全靠直觉手感? 本文中,一位 Google 程序员将“现场”演示其解决编程问题的始末,看看有套路的问题解决 ...

  8. 网易云和QQ音乐api

    最近有点闲,听歌的时候遇见好听的想下载却遇到尴尬的事,版权!唉,知道我干什么了吧 网易云音乐 获取排行榜中的歌曲列表 https://music.163.com/discover/toplist?id ...

  9. PHP实现大转盘抽奖算法实例

    本文主要向大家介绍了PHP语言实现大转盘抽奖算法,通过具体的实例向大家展示,希望对大家学习PHP抽奖有所帮助. 流程:1.拼装奖项数组,2.计算概率,3.返回中奖情况 代码如下:中奖概率 ' v ' ...

  10. MSMQ 队列消息和死信队列

    日志消息 日志队列可以保存你操作过的消息的备份.它的好处是,一但发现前面的操作失败,可以从日志队列中重新创建出原先的消息对象,然后再进行操作. 例如,向远方发送一个消息对象,然后对方返回一个失败的确认 ...