Leetcode Array 16 3Sum Closest
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 len = nums.length;
int ans = 0;
boolean bans = false;
Arrays.sort(nums);
for(int i=0;i<len-1;i++){
if(i>0 && nums[i] == nums[i-1])
continue;
int left = i+1,right = len-1;
while(left<right){
int sum = nums[i]+nums[left]+nums[right];
if(sum == target){
return ans = sum;
}
else if( sum > target ){
right--;
}
else{
left++;
}
if(!bans){
ans = sum;
bans = true;
}
else if( Math.abs(target-ans) > Math.abs(target-sum) ){
ans = sum;
}
}
}
return ans;
}
}
Leetcode Array 16 3Sum Closest的更多相关文章
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- 【一天一道LeetCode】#16. 3Sum Closest
一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...
- 【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 ...
- LeetCode:16. 3Sum Closest(Medium)
1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...
- 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 ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
随机推荐
- css iframe边框去掉
[IE6以下] iframe边框通过css设定在FF下正常在ie下却还存在边框,通过在iframe标签内部设置属性 frameborder="no" border="0& ...
- Do not use built-in or reserved HTML elements as component id: header
刚刚在搭建项目时发现控制台报错 查找发现是因为组件名称所致,也就是当我们起名一个header.vue的组件时,我们安装的vue插件会自动把name设置为default 这就造成了错误 把header修 ...
- EasyUI中combogrid设置onSelect后 获取不到getSelecte问题解决
$('#person').combogrid({ onSelect: function(index, item){ $('#dataForm').form('load', item); $(" ...
- sql2008安装时 重新启动计算机 失败
原文发布时间为:2010-11-02 -- 来源于本人的百度文章 [由搬家工具导入] sql2008安装时 重新启动计算机 失败解决方法:regedit 运行 打开注册表,找到HKEY_LOCAL_M ...
- SQL:某个字段重复的时候只查询出最后插入的那条
原文发布时间为:2009-04-11 -- 来源于本人的百度文章 [由搬家工具导入] 数据库com中有下列数据 id,userid,tupian,time 1 3 3 2006.3.5 2 4 4 2 ...
- DBCP连接池与c3p0连接池
1. DBCP连接池
- [转]在Storyboard中使用自定义的segue类型
转自:http://my.oschina.net/u/728866/blog/92709 我们知道segue共有三种类型:push.modal以及custom.如下图: 很明显,这三种类型的作用分 ...
- golang xorm MSSQL where查询案例
xorm官方中文文档 参考 http://xorm.io/docs/ 以sqlserver为例 先初始化连接等... engine, err := xorm.NewEngine("mssql ...
- 设置jenkins的邮件通知功能
1.进入系统配置页面配置邮件发送的SMTP 2. 进入项目配置页面,配置邮件通知:(每次不稳定构建时会邮件通知)
- GLB串
题目描述 只要一个字符串中包含大写“GLB”,高老板就认为这是一个GLB串.现在给你一些字符串,请你帮高老板判断这些字符串是不是GLB串. 输入 首先是一个整数T,表示T行数据,每行一个字符串(只包括 ...