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. Nginx访问控制

    1.访问控制 涉及模块:ngx_http_access_module 模块概述:允许限制某些 IP 地址的客户端访问. 对应指令: allow 语法: allow address | CIDR | u ...

  2. .net 多线程之async await

    主线程遇到await 关键字后就交给子线程执行了 先定义一个task 可以让主线程和子线程同时执行,通过await关键字可以让主线程等待子线程执行完毕,await后面的代码可以视为异步方法的回调,可以 ...

  3. 爬虫系列2:Requests+Xpath 爬取租房网站信息

    Requests+Xpath 爬取租房网站信息 [抓取]:参考前文 爬虫系列1:https://www.cnblogs.com/yizhiamumu/p/9451093.html [分页]:参考前文 ...

  4. 总结:Java 集合进阶精讲2-ArrayList

    知识点:Java 集合框架图 总结:Java 集合进阶精讲1 总结:Java 集合进阶精讲2-ArrayList 初探: ArrayList底层结构是数组,是List接口的 可变数组的实现,所以会占用 ...

  5. ARM开发板挂载虚拟机 nfs目录

    ARM开发板做相关开发,为了调试方便,常把开发板mnt目录挂载到虚拟机nfs共享目录上,这样调试程序时候就不用把程序转到开发板上再运行,方便很多.要挂载nfs共享目录,需要安装必要的组件支持. 1.虚 ...

  6. for update 与where current of的问题

    在刚学oracle时一直不明白for update 的作用,今天考试又遇到郁闷半天,所以加以整理. 一: 1>首先for update是对表的行进行锁定.锁定就好比我们学java Thread那 ...

  7. nginx功能扩展整理

    0.基本负载均衡配置 编辑/etc/nginx/nginx.conf,加入负载平衡配置: http { upstream tomcat { server localhost:8080; server ...

  8. C++_数字时钟

    利用C++语言基础,制作了一个模拟电子时钟的程序. #include<iostream> #include<windows.h> //延时与清屏头文件 using namesp ...

  9. sys模块学习记录

    import sys s = sys.argv #命令行参数List,第一个元素是程序本身路径 #sys.exit() #退出程序,正常退出时exit(0) s = sys.version #获取Py ...

  10. C++的ch1&ch2的整理

    C++:带泪的C,意指学C语言的时候没有好好学,在学习此门课时会流下不学无术的泪水(仅对于我个人). 计算机程序语言的发展:机器语言[计算机可以识别的二进制指令]——>汇编语言[将机器指令转化为 ...