题目:

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).

思路:

和3sum一样,排序之后,先确定第一个元素,然后对余下元素进行两边夹。

package sum;import java.util.Arrays;public class ThreeSumClosest {public int threeSumClosest(int[] nums, int target) {int len = nums.length;Arrays.sort(nums);int delta = nums[0] + nums[1] + nums[2] - target;for (int i = 0; i < len - 2;) {int l = i + 1;int r = len - 1;while (l < r) {int tmp = nums[i] + nums[l] + nums[r] - target;if (Math.abs(tmp) < Math.abs(delta))delta = tmp;if (tmp == 0)return target;if (tmp < 0)++l;else--r;}do { ++i; } while (i < len && nums[i] == nums[i - 1]);}return target + delta;}public static void main(String[] args) {// TODO Auto-generated method stubint[] nums = { -1, 2, 1, -4 };int target = 1;ThreeSumClosest t = new ThreeSumClosest();System.out.println(t.threeSumClosest(nums, target));}}

转载于:https://www.cnblogs.com/null00/p/5025899.html

LeetCode - 3Sum Closest相关推荐

  1. [LeetCode] 3Sum Closest

    依旧先来题目: Given an array S of n integers, find three integers in S such that the sum is closest to a g ...

  2. [LeetCode]3Sum Closest

    题目 Number: 16 Difficulty: Medium Tags: Array, Two Pointers Given an array S of n integers, find thre ...

  3. LeetCode算法入门- 3Sum Closest -day10

    LeetCode算法入门- 3Sum Closest -day10 Given an array nums of n integers and an integer target, find thre ...

  4. LeetCode - 16. 3Sum Closest

    16. 3Sum Closest Problem's Link -------------------------------------------------------------------- ...

  5. leetcode 16 -- 3Sum Closest

    3Sum Closest 题目: Given an array S of n integers, find three integers in S such that the sum is close ...

  6. Leetcode | 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  7. 3Sum 3Sum Closest 4Sum

    难度:3 3Sum的题意: 给出一列数,找出所有的满足条件的三个数,条件:三个数相加等于0 3Sum Closest 的题意: 给出一列数,找出三个数使得三个数相加最接近target 4Sum: 和3 ...

  8. Leetcode 742. Closest Leaf in a Binary Tree

    Leetcode 742. Closest Leaf in a Binary Tree Approach #1: Convert to Graph 把树转换为以k节点为中心的图,从k节点开始层序遍历, ...

  9. 【算法】3Sum Closest 最接近的三数之和

    文章目录 3Sum Closest 最接近的三数之和 问题描述: 分析 代码 二分 双指针 Tag 3Sum Closest 最接近的三数之和 问题描述: 给你一个长度为 n 的整数数组 nums 和 ...

最新文章

  1. 计算机组成原理与系统结构---内存编址方法
  2. KVM嵌套虚拟化 -- 在虚拟机中创建虚拟机
  3. 【高效JDBC编程工具JadePool快速入门】
  4. xilinx fpga 开发工具vivado 软件的安装,使用详细教程
  5. servlet容器_Servlet详解(一)之基本概念
  6. olr 性能调优 NO_NORMS
  7. 玩转oracle 11g(47):oracle删除非空表空间
  8. python mysql操作封装库_python封装mysq操作,进行数据库的增删改
  9. IBM heapAnalyzer
  10. 二叉树学习笔记之利用前序遍历递归创建二叉树
  11. Oracle 11g EM删除重建的方法
  12. GoodTask for mac (任务管理器)
  13. html网页简单代码
  14. Micrium uC-Probe STM32调试工具 ucosIIucosIII
  15. 字体加密网站的抓取以及思路解决
  16. 自古英雄出少年,22岁中国小哥哥入选Nature十大人物
  17. 借助小程序·云开发制作校园导览小程序丨实战
  18. 智能结算新方案,这个商品识别算法你值得拥有!
  19. 软件项目延期,怎么办?
  20. 好友推荐(列转行,help_topic_id)

热门文章

  1. kafka副本数据同步策略
  2. ip访问php $_files空,PHP中表单没有问题但$_FILES为空怎么办?
  3. c 多线程map_Rust:一个不再有 C/C++ 的,实现安全实时软件的未来
  4. Mybatis Integer类型参数值为0时判断为空、空字符串不通过
  5. Android 第一课 Activity
  6. 什么是设计模式_什么是设计?
  7. 给div拼接html 拼接字符串
  8. BZOJ1562: [NOI2009]变换序列(二分图 匈牙利)
  9. Nginx服务状态的监控
  10. JavaScriptSerializer类 对象序列化为JSON,JSON反序列化为对象