题目链接:https://leetcode.com/problems/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的数。

演示样例代码:

import java.util.Arrays;
public class Solution
{
public int threeSumClosest(int[] nums, int target)
{
int length=nums.length;
int minNum=Integer.MAX_VALUE;
int tempsubNum=Integer.MAX_VALUE;
Arrays.sort(nums);//先对nums进行排序
for(int i=0;i<length-2;i++)
{
//略过同样的数
if(i!=0&&nums[i]==nums[i-1])
{
continue;
}
int left=i+1;
int right=length-1;
while(left<right)
{
int a1=nums[left];
int a2=nums[right];
int sum=a1+a2+nums[i];
int subNum=Math.abs(sum-target);
//当前的和值sum离target更近一点
if(subNum<=tempsubNum)
{
tempsubNum=subNum; //保存这一步中的sum-target的差值
minNum=sum;
}
if(sum-target<0) //说明sum<target,
left++;
else
right--;
}
}
return minNum;
}
}

【LeetCode OJ 016】3Sum Closest的更多相关文章

  1. 【LeetCode OJ 232】Implement Queue using Stacks

    题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...

  2. 【LeetCode OJ 136】Single Number

    题目链接:https://leetcode.com/problems/single-number/ 题目:Given an array of integers, every element appea ...

  3. 【LeetCode OJ 268】Missing Number

    题目链接:https://leetcode.com/problems/missing-number/ 题目:Given an array containing n distinct numbers t ...

  4. 【LeetCode OJ 34】Search for a Range

    题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...

  5. 【LeetCode OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

  6. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  7. 【LeetCode算法-27】Remove Element

    LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...

  8. 【leetcode】3Sum Closest

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  9. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

随机推荐

  1. thinkpad win8.1 无线连接受限

    把博通的驱动回滚到6.30.223.102就好了

  2. URAL 2072 Kirill the Gardener 3 (单调DP)

    [题目链接] http://acm.timus.ru/problem.aspx?space=1&num=2072 [题目大意] 一个园丁要给一排花浇水,每个花都有一个标号,必须要先浇标号小的, ...

  3. 【数论】【暴力】bzoj4052 [Cerc2013]Magical GCD

    考虑向一个集合里添加一个数,它们的gcd要么不变,要么变成原gcd的一个约数.因此不同的gcd只有log个. 所以对于每个位置,维护一个表,存储从这个位置向前所有的不同的gcd及其初始位置,然后暴力更 ...

  4. 【离散化】【扫描线】CH Round #59 - OrzCC杯NOIP模拟赛day1 队爷的新书

    //上图绿色扫描线右侧少画了一条扫描线. 很多区间把数轴分成了很多段,看哪个点的(区间覆盖数*该点权值)最大. 显然在某个区间的右端点的答案是最优的. 排序后 用扫描线从左到右扫描,维护每个点的覆盖数 ...

  5. python3中zipfile模块的常用方法

    一.zipfile模块的简述 zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是很常见的zip格式,所以这个模块使用频率也是比较高的, 在这里对zipfile的使用方法做一些记 ...

  6. JNI之数组

    Array Operations -- 数组操作 1.GetArrayLength jsize GetArrayLength(JNIEnv *env, jarray array); Returns t ...

  7. SQL SERVER 常用命令

    红色为常用 0.row_number() over 和数据组合sale/cnt select *,row_number() over(order by productname) as rownumbe ...

  8. delphi杀进程的两种方式

    delphi杀进程的两种方式 uint unit Tlhelp32; 第一种:比较简单,根据标题,找到窗口,再找到进程,杀死进程 procedure KillProgram(WindowTitle : ...

  9. 深入了解JavaScript权威指南

    JavaScript对象可以看作是属性的无序集合,每个属性就是一个键值对,可增可删.JavaScript中的所有事物都是对象:字符串.数字.数组.日期,等等.JavaScript对象除了可以保持自有的 ...

  10. javascript快速入门22--Ajax简介

    Ajax是什么? 首先,Ajax是什么?一个很酷的新兴词汇!仅仅是某种早就有了的技术的一种新说法而已! Ajax是指一种创建交互式网页应用的网页开发技术.要谈到网页应用程序,则必须从WEB的历史来讲: ...