3 sum 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).
找到数组中跟目标数最接近的三个数的和 。
这里也是三个数的和问题,经过上一题三个数的和,这一题结果只有一个,也会有重复数字出现,需要处理,当然了,这里只要找到近似的和,不处理重复不影响结果。。
这里也是遍历数组,然后在剩下的数组中依次遍历(两端向中间),每次遍历都会产生一个三数之和,如果该和大于target,则需要右边指针向左移动,反之左边向右移动。同时也要判断此时的和与target之间的距离跟result与target之间距离的大小。
class Solution {
public int threeSumClosest(int[] nums, int target) {
/**
这里也是三个数的和问题,经过上一题三个数的和,这一题结果只有一个,也会有重复数字出现,需要处理,当然了,这里只要找到近似的和,不处理重复不影响结果。。
这里也是遍历数组,然后在剩下的数组中依次遍历(两端向中间),每次遍历都会产生一个三数之和,如果该和大于target,则需要右边指针向左移动,反之左边向右移动。同时也要判断此时的和与target之间的距离跟result与target之间距离的大小。
*/
//初始一个三个元素的和的变量,下面计算三个数的和时要和这个以及target之间的差值作比较
int result=nums[0]+nums[1]+nums[nums.length-1];
Arrays.sort(nums);
for(int i=0;i<nums.length-2;i++){
//两个数的和与sum最接近就行
int left=i+1,right=nums.length-1;
while(left<right){
int sum=nums[i]+nums[left]+nums[right];
if(sum>target) right--;
else left++;
if(Math.abs(result-target)>Math.abs(sum-target))
result=sum;
}
}
return result;
}
}
3 sum closest的更多相关文章
- Lintcode: Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...
- Subarray Sum Closest
Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the ...
- 3 Sum Closest 解答
Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- [leetcode]3 Sum closest
问题叙述性说明: Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST
▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...
- (hash map)Two Sum, sorted(排序+双指针)closest,小于或大于的对数,组成不同的对数
原版 sorted [抄题]: [思维问题]: 存sum - nums[i](补集),若出现第二次则调出 [一句话思路]: hashmap中,重要的数值当做key,角标当做value. [画图]: [ ...
- No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- 3Sum Closest & 3Sum Smaller
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [leetcode]_K Sum 问题
问题:K Sum问题是一个问题系列,在一个数组中找K个数的和能够满足题目中要求.从2 Sum 到 3 Sum , 3 Sum Clozet , 4 Sum..解法虽一开始不容易想到,但get到解题技能 ...
随机推荐
- Simple tutorial for using TensorFlow to compute polynomial regression
"""Simple tutorial for using TensorFlow to compute polynomial regression. Parag K. Mi ...
- 自定义android 4.0以上的对话框风格
做个笔记,这里是Dialog的风格,如果是用AlertDialog创建的,不能直接用.在styles.xml的写法: <style name="DialogWindowTitle&qu ...
- UNIX网络编程——TCP回射服务器/客户端程序
下面通过最简单的客户端/服务器程序的实例来学习socket API. serv.c 程序的功能是从客户端读取字符然后直接回射回去: #include<stdio.h> #include&l ...
- WebLogic11g-负载分发
负载均衡的实现方式有很多种,这里只介绍三种相对来说成本较低的方案(维护成本以及费用成本)weblogic自带的proxy.apache.nginx 1.weblogic自带的proxy方式: 1) ...
- 关于APP界面布局设计的八种优缺点
学习UI设计的时候,经常要接触到页面的布局,布局的方式会直接影响一个APP的视觉效果,好的布局方式,往往能带来舒服的视觉效果,更能得到用户的接受与好评.然而万变不离其宗,移动端页面常用的布局,不外乎以 ...
- Unity UGUI基础之InputField
InputField(输入域):为文本输入控件,等同于NGUI的Input. 一.InputField组件: Text Component(文本组件):此输入域的文本显示组件,需带有Text组件. T ...
- Linux中的查找命令find
原文:http://blog.csdn.net/windone0109/article/details/2817792 查找目录:find /(查找范围) -name '查找关键字' -type d ...
- Android项目-高考作文项目架构(三)
上一篇我们讲到了, Http Json的功能的抽取. 如果我们请求的是一个列表的数据呢? 我们使用那个功能就不是很好. 因为一个列表, 还有很多其他功能(比如每个listView都需要setAdap ...
- 程序员需要有多懒 ?- cocos2d-x 数学函数、常用宏粗整理
原帖地址:http://www.cnblogs.com/buaashine/archive/2012/11/12/2765691.html 1.注意这是cocos2d-x中的函数,但大体上和cocos ...
- 调用sed命令的三种方式
调用sed命令的三种方式 调用sed有三种方式,一种为Shell命令行方式,另外两种是将sed命令写入脚本文件,然后执行该脚本文件. 三种方式的命令格式归纳如下: 一.在Shell命令行输入命令调用s ...