【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 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取绝对值,每次找出误差最小值及对应的三个数的和。
public class Solution {
public int threeSumClosest(int[] nums, int target) {
int len=nums.length;
int closestVal=Integer.MAX_VALUE;
int closestSum=nums[0]+nums[1]+nums[2];
Arrays.sort(nums);
int currentNum=nums[0];
for(int i=0;i<len-2;i++){
if(i>0 && nums[i]==currentNum) continue;
int begin=i+1,end=len-1;
while(begin<end){
int sum=nums[i]+nums[begin]+nums[end];
int absVal=Math.abs(sum-target);
if(absVal<closestVal){
closestVal=absVal;
closestSum=sum;
}else if(sum<target){
begin++;
}else end--;
}
currentNum=nums[i];
}
return closestSum;
}
}
【LeetCode】16. 3Sum Closest的更多相关文章
- 【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】016 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- [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】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- 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 num ...
- 【LeetCode】973. K Closest Points to Origin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...
随机推荐
- 如何设置DNS的SPF记录
如何设置DNS的SPF记录 Introduction SPF的完整意思为 "Sender Policy Framework".翻译过来就是发送方策略框架,是一项跟 DNS 相关的技 ...
- Learning Puppet — Resources and the RAL
Learning Puppet — Resources and the RAL Welcome to Learning Puppet! This series covers the basics of ...
- 242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- PHP注释有意思的排列
<?php // // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // O\ = /O // ____/`---'\___ ...
- 访问public
public(C# 参考) public 关键字是类型和类型成员的访问修饰符. 公共访问是允许的最高访问级别. 对访问公共成员没有限制,如下例所示: class SampleClass { publi ...
- 黄聪:Discuz!的SEO优化策略一:如何设置标题 & 如何去掉Powered by Discuz!尾巴
1.如何设置标题 进入 管理中心 -- 全局 -- SEO设置 -- 论坛 -- 标题 ,设置你的网站标题和描述. PS:有人问keywords要不要设置,其实现在keywords对SEO的影响已经微 ...
- Akka(二) - Future
1. future的所有方法都是非阻塞立即返回的 (1)future都要有TimeOut和ExecutionContextExecutor这2个隐士参数 (2)打印future object Hell ...
- [CSS]去除inline-block元素间距的几种方法
当我们使用inline-block 时,会出现空白间距问题. 但这些间距对我们的布局,或兼容性产生影响,我们需要去除它,该怎么办?下面简单介绍几种方法: 1.去掉html元素之间的空格,直接写在一行. ...
- MongoDB的基本使用
use library 使用use函数切换已有的数据库或创建新的数据库 show dbs 查看MongoDB中目前所有可用的数据库 show collections 查看当前数据库中的所有集合 在集合 ...
- C#(类)
一.String类 string s = " abCDefgb ";int a = s.Length;//获取长度 Console.WriteLine(s.Length); Con ...