问题: 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).
复制代码

方法: 最原始的方法是三层循环遍历,求所有三个数的sum,然后找出最接近target的sum,但是这样的算法复杂度比较高,需要O(n^3)的时间复杂度,通过排序可以降低比较的次数,先对数组进行排序,因为数组有序,如果sum大于target就从后面移动index,如果sum小于target就从前面移动index,向中间逼近,这样就可以减少比较的次数,这样的算法时间复杂度是O(n^2)

具体实现:

class ThreeSumClosest {// -4, -1, 2, 1,// 双逼近算法fun threeSumClosest(nums: IntArray, target: Int): Int {var sum = nums[0] + nums[1] + nums[2]nums.sort()for (x in nums.indices) {var i = x + 1var j = nums.lastIndexwhile (i < j) {val curSum = nums[i] + nums[j] + nums[x]val curDiff = abs(sum - target)val diff  = abs(curSum - target)if (diff < curDiff) {sum = curSum}if (curSum - target > 0) {j--} else if(curSum - target < 0) {i++} else {return sum}}}return sum}
}fun main(args: Array<String>) {val nums = intArrayOf(0,2,1,-3)val target = 1val threeSumClosest = ThreeSumClosest()println(threeSumClosest.threeSumClosest(nums, target))
}
复制代码

有问题随时沟通

具体代码实现可以参考Github

LeetCode之3Sum Closest(Kotlin)相关推荐

  1. LeetCode 16 3Sum Closest(最接近的3个数的和)

    翻译 给定一个有n个整数的数组S,找出S中3个数,使其和等于一个给定的数,target.返回这3个数的和,你可以假定每个输入都有且只有一个结果.例如,给定S = {-1 2 1 -4},和target ...

  2. LeetCode - 16. 3Sum Closest

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

  3. leetcode 16 -- 3Sum Closest

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

  4. 【LeetCode】3Sum Closest 解题报告

    [题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...

  5. [LeetCode][Java] 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  6. LeetCode——16. 3Sum Closest

    一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...

  7. LeetCode 16 3Sum Closest

    问题:给出一个数组nums,及目标数target,要求找出数组中三个数之和与target最接近的数 思路:第一种方法是使用三种循环,依次遍历,看三个数之和与目标数最拉近,同时更新.该算法时间复杂度为O ...

  8. leetcode 16. 3Sum Closest | 16. 最接近的三数之和(双指针)

    题目 https://leetcode.com/problems/3sum-closest/ 题解 方法1:固定 L,双指针找 M.R 时间复杂度 O(n^2),推荐此方法. 证明不会有元素遗漏,详见 ...

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

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

最新文章

  1. 基于ip地址的客户识别原理_使用 LVS 实现负载均衡原理及安装配置
  2. 陆奇首批YC中国门徒:包鱼塘、画动漫、搞汽配…都用AI
  3. python xmxl 无法启动_Python小白到老司机,快跟我上车!基础篇(三)
  4. java通过使用ffmpeg获取视频的码率
  5. go语言中fallthrough与break的使用
  6. HTML基础(part7)--常用标签之超链接标签
  7. 深入理解C++ 虚函数表
  8. curl和file_get_contents 区别以及各自的优劣
  9. C# Parse和Convert的区别分析
  10. 如果计算机正执行屏幕保护程序 当用户,计算机1级考试参考试题(含答案)章节1.doc...
  11. 通过PyMuPDF编写增值税发票多PDF文件合并工具
  12. 未对销售组织 XXX 分销渠道 XX 语言 ZH 定义
  13. nrcellcu和nrcelldu_华为5G常用MML命令(现场版本)
  14. Vivo 监控系统演进之路
  15. 【Cisco(思科)路由器vlan配置实例】制作简单的B项目小组VLAN规划
  16. Ardunio测试817光电耦合交流电过零检测模块
  17. C#访问大华网络摄像头
  18. 有用的SAP系统管理事务码
  19. python pandas 实战 百度音乐歌单 数据分析
  20. 2022年计算机领域EI检索国际学术会议参考列表(AI/CV/机器人等领域)

热门文章

  1. 标准SPI、DUAL SPI、Quad SPI
  2. SPI通信实验---verilog(FPGA作为从机,使用可读可写)
  3. Scrapy中的Spider
  4. tortoisegit 代码的回滚方式 --两种
  5. 前端之JavaScript进阶
  6. Eclipse 快捷键整理
  7. Windows8 商店支付功能
  8. c++时间函数及转换
  9. *args和**kargs
  10. MFC列表控件(ListControl)