描述

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. Given array S = {-1 2 1 -4}, and target = 1.
  2. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

算法分析

难度:中

分析:给定整型数组中和一个指定的目标整型值,从数组中找到3个元素,满足3个元素之和最接近目标值,返回结果为3个元素之和。

思路:大体思路类似3Sum算法,也是先将数组排序,然后开始遍历,3个元素分别是当前遍历的元素、夹逼开始元素默认当前元素后面的元素,夹逼结束元素默认数组最后的元素,通过夹逼开始元素递增和夹逼结束元素递减来实现夹逼:

 1. 如果这3个元素之和比目标值大,则需要夹逼结束元素值变小,才有可能接近目标值,所以夹逼结束元素递减;

 2. 如果这3个元素之和不比目标值大,则需要夹逼开始元素值变大,才有可能接近目标值,所以夹逼开始元素递增;

本次遍历结束后,再判断本次3元素之和目前记录的最接近之和比较,取更接近的做为返回结果,然后继续遍历,直至遍历结果,获得返回结果。

代码示例(C#)

  1. public int ThreeSumClosest(int[] nums, int target)
  2. {
  3. int res = nums[0] + nums[1] + nums[nums.Length - 1];
  4. //排序后遍历
  5. Array.Sort(nums);
  6. for (int i = 0; i < nums.Length - 2; i++)
  7. {
  8. //从当前后面的元素和最后一个元素,两边夹逼
  9. int lo = i + 1, hi = nums.Length - 1;
  10. while (lo < hi)
  11. {
  12. int sum = nums[i] + nums[lo] + nums[hi];
  13. if (sum > target)
  14. {
  15. hi--;
  16. }
  17. else
  18. {
  19. lo++;
  20. }
  21. //如果此次遍历的3个元素的和更接近,则赋值返回的结果
  22. if (Math.Abs(sum - target) < Math.Abs(res - target))
  23. {
  24. res = sum;
  25. }
  26. }
  27. }
  28. return res;
  29. }

复杂度

  • 时间复杂度O (n²).
  • 空间复杂度O (1).

附录

算法题丨3Sum Closest的更多相关文章

  1. 算法题丨3Sum

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

  2. 算法题丨Two Sum

    描述 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  3. 算法题丨4Sum

    描述 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  4. 算法题丨Remove Duplicates from Sorted Array II

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...

  5. 算法题丨Longest Consecutive Sequence

    描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...

  6. 算法题丨Remove Element

    描述 Given an array and a value, remove all instances of that value in-place and return the new length ...

  7. 算法题丨Move Zeroes

    描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...

  8. 算法题丨Next Permutation

    描述 Implement next permutation, which rearranges numbers into the lexicographically next greater perm ...

  9. 算法题丨Remove Duplicates from Sorted Array

    描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...

随机推荐

  1. sql语句转为Model

    在跟数据库打交道的时候,有一个常用的应用,就是把数据库中的表转为程序中的对象,也就是说表中的列转为对象的属性.对于字段比较少的,我们可以直接复制过去改,但是字段数比较多的时候,借助工具类实现比较方便而 ...

  2. FPGA加三移位算法:硬件逻辑实现二进制转BCD码

    本文设计方式采用明德扬至简设计法.利用FPGA来完成显示功能不是个很理想的方式,当显示任务比较复杂,要通过各种算法显示波形或者特定图形时,当然要用单片机通过C语言完成这类流程控制复杂,又对时序要求不高 ...

  3. 面向服务的体系架构 SOA(二) --- 服务的路由和负载均衡

    2. 服务的路由和负载均衡 1.2.1 服务化的演变 SOA设计思想:分布式应用架构体系对于业务逻辑复用的需求十分强烈,上层业务都想借用已有的底层服务来快速搭建更多.更丰富的应用,降低新业务开展的人力 ...

  4. Docker(二):Dockerfile 使用介绍

    上一篇文章Docker(一):Docker入门教程介绍了 Docker 基本概念,其中镜像.容器和 Dockerfile .我们使用 Dockerfile 定义镜像,依赖镜像来运行容器,因此 Dock ...

  5. 微信小程序腾讯云php后台解决方案

    微信小程序腾讯云php后台解决方案 微信小程序前段需要添加必要的文件以配合后端 (1)wafer2-client-sdk sdk提供了几种接口包括登陆,获取用户openid,图片上传等 (2)conf ...

  6. 使用Python解析豆瓣上Json格式数据

    现在的API接口多为xml或json,json解析更简洁相对xml来说 以豆瓣的API接口为例,解析返回的json数据: https://api.douban.com/v2/book/1220562 ...

  7. 【Unity3D与23种设计模式】模板方法模式(Template Method)

    GoF中定义: "在一个操作方法中定义算法的流程,其中某些步骤由子类完成. 模板方法模式让子类在不变更原有算法流程的情况下,还能够重新定义其中的步骤" 每一次武器攻击目标时,都要按 ...

  8. fastjson从1.1.41升级到1.2.28的坑

    最近因为fastjson安全漏洞,升级jar包时,踩了一些坑. 新版本FastJsonHttpMessageConverter初始化,默认设置MediaType为*/* 背景: 使用Spring Re ...

  9. kubernetes实现用户自定义扩缩容

    本文章主要参考walkthrough,aggregation和auth.涉及custom metric API的注册认证以及API server aggregation的相关知识.walkthroug ...

  10. 总结的Javascript插件

    1.很好用的弹窗 https://limonte.github.io/sweetalert2/ https://github.com/limonte/sweetalert2 import './unt ...