zoukankan      html  css  js  c++  java
  • LeetCode OJ: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).

    求三个数的和sum,使sum与target相差最小。这题实际上跟前面坐的2Sum,以及3Sum相差不是很大,大体的想法是先排序,然后外层的for循环遍历从小到大的数,然后从剩下的数中取三数之和,然后把最小的一步步保存下来,最终将最小的返回就行了。代码如下:
     1 class Solution {
     2 public:
     3     int threeSumClosest(vector<int>& nums, int target) {
     4         int sz = nums.size();
     5         sort(nums.begin(), nums.end());//当然了,首先还是要排序的
     6         int diff = INT_MAX;//这个直接取INT_MAX,这样计算出来的diff一定是比这个要小的
     7         int tmpDiff;
     8         int ans;
     9         int beg, end;
    10         for (int i = 0; i < sz - 2; ++i){
    11             beg = i + 1, end = sz - 1;
    12             while (beg < end){
    13                 int sum = nums[i] + nums[beg] + nums[end];
    14                 if ((tmpDiff = abs(sum - target)) < diff){
    15                     diff = tmpDiff;
    16                     ans = sum;
    17                 }
    18                 if (sum > target){
    19                     end--;
    20                 }
    21                 else if (sum < target){
    22                     beg++;
    23                 }
    24                 else
    25                     return sum;//相等的话就直接返回了
    26             }
    27         }
    28         return ans;
    29     }
    30 };

    大体上就是这样

    java版本的如下所示,基本上与上面的没有什么区别:

     1 public class Solution {
     2     public int threeSumClosest(int[] nums, int target) {
     3          int len = nums.length;
     4          int ret = 0;
     5          int diff = Integer.MAX_VALUE;
     6          Arrays.sort(nums);
     7         for(int i = 0; i < len - 2; ++i){
     8             int beg = i + 1;
     9             int end = len - 1;
    10             while(beg < end){
    11                 int tmp = nums[i] + nums[beg] + nums[end];
    12                 int tmpDiff  = Math.abs(tmp - target);
    13                 if(tmpDiff < diff){
    14                     ret = tmp;
    15                     diff = tmpDiff;
    16                 }
    17                 if(tmp < target)
    18                     beg++;
    19                 else if(tmp > target)
    20                     end--;
    21                 else
    22                     return target;
    23             }
    24         }   
    25         return ret;
    26     }
    27 }
  • 相关阅读:
    Struts2与Ajax数据交互
    Struts2笔记--文件下载
    Struts2笔记--文件上传
    Struts2笔记--Action访问Servlet API
    Servlet笔记2-文件上传
    Listener监听器笔记1
    ios开发 "此证书的签发者无效"
    WinObjC 微软搞了一个这个Windows Bridge for iOS,吸引iOS开发者; 表示很期待
    unity与iOS、Android交互
    iOS 9检测QQ、微信是否安装
  • 原文地址:https://www.cnblogs.com/-wang-cheng/p/4857149.html
Copyright © 2011-2022 走看看