题目链接: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. 1.6(java学习笔记)static关键字

    static关键字 1.static修饰变量也称静态变量,静态变量存放在静态区被该类的所有对象共享. 例如,定义了一个类class User{static Sring city = "a城& ...

  2. IntelliJ 常用设置

    一.智能代码提示忽略大小写 打开设置(CTRL+ALT+S)搜索editor,找到“Code Completion”->点击Case sensitive completion后面的选择框,选中N ...

  3. [转] matlab调用opencv函数的配置

    原文地址百度账户 aleasa123 方式1 1.  首先保证vs2010能正确调用opencv函数, 2.  Matlab中选择编译器,操作如下: 打开matlab2012,输入mex –setup ...

  4. java只有值传递,不存在引用传递

    今天,我在一本面试书上看到了关于java的一个参数传递的问题: 写道 java中对象作为参数传递给一个方法,到底是值传递,还是引用传递? 我毫无疑问的回答:“引用传递!”,并且还觉得自己对java的这 ...

  5. osgMulitiplerendertargets sample 中fbo使用【HTC VIVE开发中应用】

    osgmultiplerendertargets.cpp ...................................... // now create the camera to do t ...

  6. Spring(八)编码剖析@Resource注解的实现原理

    配置文件beans2.xml <?xml version="1.0" encoding="UTF-8"? > <beans xmlns=&qu ...

  7. JMS-activeMq发布订阅模式(非持久订阅)

    Publisher的代码: import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Deli ...

  8. 无password身份验证:安全、简单且部署高速

    Passwordless authentication: Secure, simple, and fast to deploy [编者按]本文作者为 Florian Heinemann 与 Rober ...

  9. POJ 3083:Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...

  10. Linux学习笔记 (六)用户管理命令

    一.用户帐号 1.超级用户:具有操作系统中的最高权限,用来管理和维护操作系统.root用户. 2.普通用户:由root用户来创建,在宿主目录中具有完全权限. 3.程序用户:由应用程序添加,维护某个应用 ...