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, ...
随机推荐
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D 数学+(前缀 后缀 预处理)
D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- [Linux]方便openmp等程序的类似编译
因为总是打参数例如-fopenmp或者-lGL等等有些麻烦,所以特地写一个可以使用的bash文件用来执行简单的操作 首先在.profile中添加 if [ -d "$HOME/bin&quo ...
- 纯js实现网页tab选项卡切换效果
纯js实现网页tab选项卡切换效果 百度搜索 js 点击菜单项就可以切换内容的效果
- react自定义组件属性类型检测
react当中的props-type用来检测传入组件当中的数据是否符合组件的要求,但是之前的只是能做些简单常规的判断,如果需要做复杂的判断,就需要使用到自定义函数来做类型检测了. 下面是官网的例子 c ...
- sql2008安装时 重新启动计算机 失败
原文发布时间为:2010-11-02 -- 来源于本人的百度文章 [由搬家工具导入] sql2008安装时 重新启动计算机 失败解决方法:regedit 运行 打开注册表,找到HKEY_LOCAL_M ...
- PE笔记之NT头PE文件头
typedef struct _IMAGE_FILE_HEADER { WORD Machine; //014C-IMAGE_FILE ...
- Netbeans 8.2启动参数含义及配置
在manjaro linux中Netbeans8.2 + JDK 1.8 netbeans的配置文件具体在:/usr/share/netbeans/etc/netbeans.conf,需要使用root ...
- iOS数组去重的方法,超级简单
//最近新发现的一个数组去重,用不着循环,一句代码搞定 //去除数组中重复的 NSArray *oldArr = @[@"1",@"2",@"3&qu ...
- 16Aspx.com-书通网中小学生免费在线学习网站源码 带采集带手机版帝国cms内核
=============================================== 源码站长资源交易专业网-商业源码下载,VIP源码,程序交易,毕业设计交易,站长交易|- 16aspx.c ...
- UVA 272 TEX Quotes【字符串】
https://vjudge.net/problem/UVA-272 [分析]:标记一下. [代码]: #include <bits/stdc++.h> using namespace s ...