题目链接: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.

  1. For example, given array S = {-1 2 1 -4}, and target = 1.
  2.  
  3. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

解题思路:先对数组进行排序,然后遍历数组。寻找和最接近target的数。

演示样例代码:

  1. import java.util.Arrays;
  2. public class Solution
  3. {
  4. public int threeSumClosest(int[] nums, int target)
  5. {
  6. int length=nums.length;
  7. int minNum=Integer.MAX_VALUE;
  8. int tempsubNum=Integer.MAX_VALUE;
  9. Arrays.sort(nums);//先对nums进行排序
  10. for(int i=0;i<length-2;i++)
  11. {
  12. //略过同样的数
  13. if(i!=0&&nums[i]==nums[i-1])
  14. {
  15. continue;
  16. }
  17. int left=i+1;
  18. int right=length-1;
  19. while(left<right)
  20. {
  21. int a1=nums[left];
  22. int a2=nums[right];
  23. int sum=a1+a2+nums[i];
  24. int subNum=Math.abs(sum-target);
  25. //当前的和值sum离target更近一点
  26. if(subNum<=tempsubNum)
  27. {
  28. tempsubNum=subNum; //保存这一步中的sum-target的差值
  29. minNum=sum;
  30. }
  31. if(sum-target<0) //说明sum<target,
  32. left++;
  33. else
  34. right--;
  35. }
  36. }
  37. return minNum;
  38. }
  39. }

【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. [CF627D]Preorder Test

    题目大意: 一个$n(n\le2\times10^5)$个结点的树,每个结点有一个权值$w_i$.可以任选一点为根,并选择一些结点交换其子结点的顺序,使得该树DFS序上第$m$个结点的权值最大.求最大 ...

  2. UC算法笔试题

    说实话,昨天UC的笔试题基本全是基础,但是太基础,直接导致很多都不能确定了.看来不管找工作还是找实习,一定要复习到位.好在我也一直是抱着打酱油的味道,实习与否不是特别在意,否则真心要鄙视死自己啦. 好 ...

  3. 使用virtualenv为应用提供了隔离的Python运行环境

    在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-packages目录下. 如果我们要同时开发多个应用程序,那这 ...

  4. 如何让vs2017 EF实体生成支持Mysql 和 Oracle?

    1.Mysql 安装   MySql Connector/Net        https://dev.mysql.com/downloads/connector/net/ 安装    mysql f ...

  5. 重设Windows 7密码 z

    Restart the computer to boot using the CD. Once the GUI loads, press SHIFT+F10 to bring up the comma ...

  6. Swift,字典

    1.创建(Dictionary)字典(无序的可重复) (1)指定类型 var a:Dictionary<String,String>=["a":"b" ...

  7. Fillrate

    http://xionggf.com/articles/graphic/misc/mobile_gpu_term.html IMR Immediate Mode Rendering 立即渲染模式 TB ...

  8. 关于JS里的函数作用域链的总结

    在JavaScript中,函数的作用域链是一个很难理解的东西.这是因为JavaScript中函数的作用域链和其他语言比如C.C++中函数的作用域链相差甚远.本文详细解释了JavaScript中与函数的 ...

  9. spring注入之使用标签 @Autowired @Qualifier

      使用标签的缺点在于必需要有源代码(由于标签必须放在源代码上),当我们并没有程序源代码的时候.我们仅仅有使用xml进行配置. 比如我们在xml中配置某个类的属性            <bea ...

  10. 基于Spark Mllib,SparkSQL的电影推荐系统

    本文测试的Spark版本是1.3.1 本文将在Spark集群上搭建一个简单的小型的电影推荐系统,以为之后的完整项目做铺垫和知识积累 整个系统的工作流程描述如下: 1.某电影网站拥有可观的电影资源和用户 ...