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

    题目标签:Array

      这道题目给了我们一个nums array 和一个 target, 让我们找到一组三数之和,并且是最接近于target的。由于我们做过了三数之和,所以差不多是一样方法来做这道题(这里充分说明了,要老老实实顺着题号做下去,较先的题目都可以被用来辅助之后的题)。方法就是先sort一下array,为啥要sort呢,因为要用到two pointers 来遍历找两数之和,只有在从小到大排序之后的结果上,才能根据情况移动left 和right。 当确定好了第一个数字后,就在剩下的array里找两数之和,在加上第一个数字,用这个temp_sum减去target 来得到temp_diff,如果temp_diff比之前的小,那么更新diff 和 closestSum。 利用two pointers 特性, 如果temp_sum 比target 小的话,说明我们需要更大的sum,所以要让left++以便得到更大的sum。 如果temp_sum 比target 大的话,我们就需要更小的sum。如果相等的话,直接return 就可以了。因为都相等了,那么差值就等于0,不会有差值再小的了。

    Java Solution:

    Runtime beats 86.06% 

    完成日期:07/12/2017

    关键词:Array

    关键点:利用twoSum的方法,two pointers 来辅助,每次确定第一个数字,剩下的就是找两个数字之和的问题了

     1 public class Solution 
     2 {
     3     public int threeSumClosest(int[] nums, int target) 
     4     {
     5         // sort the nums array
     6         Arrays.sort(nums);
     7         int closestSum = 0;
     8         int diff = Integer.MAX_VALUE;
     9         
    10         
    11         // iterate nums array, no need for the last two numbers because we need at least three numbers
    12         for(int i=0; i<nums.length-2; i++)
    13         {
    14             int left = i + 1;
    15             int right = nums.length - 1;
    16             
    17             // use two pointers to iterate rest array
    18             while(left < right)
    19             {
    20                 int temp_sum = nums[i] + nums[left] + nums[right];
    21                 int temp_diff = Math.abs(temp_sum - target);
    22                 // if find a new closer sum, then update sum and diff
    23                 if(temp_diff < diff)
    24                 {
    25                     closestSum = temp_sum;
    26                     diff = temp_diff;
    27                 }
    28                 
    29                 if(temp_sum < target) // meaning need larger sum
    30                     left++;
    31                 else if(temp_sum > target) // meaning need smaller sum
    32                     right--;
    33                 else // meaning temp_sum == target, this is the closestSum 
    34                     return temp_sum;
    35             }
    36         }
    37         
    38         return closestSum;
    39     }
    40 }

    参考资料:N/A

    LeetCode 算法题目列表 - LeetCode Algorithms Questions List

  • 相关阅读:
    CSS 的层叠上下文是什么
    BFC原理解析
    从 (a==1&&a==2&&a==3) 成立中看javascript的隐式类型转换
    IIFE中的函数是函数表达式,而不是函数声明
    Virtual DOM 真的比操作原生 DOM 快吗?
    解决for循环里获取到的索引是最后一个的问题
    bzoj4161: Shlw loves matrixI
    [NOI2007]生成树计数环形版
    bzoj1494: [NOI2007]生成树计数
    bzoj1964: hull 三维凸包
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7159535.html
Copyright © 2011-2022 走看看