zoukankan      html  css  js  c++  java
  • 0016. 3Sum Closest (M)

    3Sum Closest (M)

    题目

    Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

    Example:

    Given array nums = [-1, 2, 1, -4], and target = 1.
    
    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
    

    题意

    求一三元组,使其和最接近给定的值。

    思路

    0015. 3Sum 类似,只是修改了判定的标准。先排序,后利用two pointers并去除重复值即可。


    代码实现

    Java

    class Solution {
        public int threeSumClosest(int[] nums, int target) {
            int min = Integer.MAX_VALUE;
            int ans = 0;
            Arrays.sort(nums);
            for (int i = 0; i < nums.length - 2; i++) {
                // 去除重复的i
                if (i > 0 && nums[i] == nums[i - 1]) {
                    continue;
                }
                int j = i + 1, k = nums.length - 1;
                while (j < k) {
                    int sum = nums[i] + nums[j] + nums[k];
                    int diff = Math.abs(sum - target);
                    if (diff < min) {
                        min = diff;
                        ans = sum;
                    }
                    // 移动j、k的标准是使sum更加接近target
                    if (sum < target) {
                        j++;
                        while (j < k && nums[j] == nums[j - 1]) {
                            j++;
                        }
                    } else if (sum > target) {
                        k--;
                        while (j < k && nums[k] == nums[k + 1]) {
                            k--;
                        }
                    } else {
                        return sum;		// 特殊情况,sum正好为target
                    }
                }
            }
            return ans;
        }
    }
    

    JavaScript

    /**
     * @param {number[]} nums
     * @param {number} target
     * @return {number}
     */
    var threeSumClosest = function (nums, target) {
      let closest = 0
      let minDis = Number.MAX_SAFE_INTEGER
      nums.sort((a, b) => a - b)
      
      for (let i = 0; i < nums.length - 2; i++) {
        if (i > 0 && nums[i] == nums[i - 1]) {
          continue
        }
    
        let j = i + 1, k = nums.length - 1
        while (j < k) {
          if (j > i + 1 && nums[j] == nums[j - 1]) {
            j++
          } else if (k < nums.length - 1 && nums[k] == nums[k + 1]) {
            k--
          } else {
            let sum = nums[i] + nums[j] + nums[k]
            let dis = Math.abs(sum - target)
            if (dis < minDis) {
              minDis = dis
              closest = sum
            }
            if (sum < target) j++
            else if (sum > target) k--
            else return sum
          }
        }
      }
    
      return closest
    }
    
  • 相关阅读:
    maven打包相关
    增强现实中的摄像机成像模型
    关于sql的exists
    oracle 连接之后出现 no logged on 问题
    关于oracle用户名登录
    myeclipse的SVN插件使用
    svn客户端使用
    svn服务器端使用
    SSH整合
    spring事务管理,xml配置aop事务和注解配置aop事务
  • 原文地址:https://www.cnblogs.com/mapoos/p/13167303.html
Copyright © 2011-2022 走看看