问题: 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. Scala微服务架构 三
  2. cdr放大后内容消失了_放大镜+定时器+画笔,582KB软件就能做到,上网课的你一定能用上...
  3. Anti-If: The missing patterns--转
  4. Zynq ZC702平台 QSPI + eMMC实现
  5. 如何成立一家私募基金公司
  6. 【推荐系统】推荐系统概述
  7. Shell脚本编程----变量的使用
  8. linux 运行java jar_linux运行jar包、查看jar包进程、停止jar包
  9. Android官方开发文档Training系列课程中文版:线程执行操作之线程间通讯
  10. 页面置换算法——最佳置换算法、最近最少使用算法、先进先出算法、时钟置换算法
  11. SVN 客户端的安装与配置
  12. 三言两语聊Python模块–文档测试模块doctest
  13. Xray配合awvs漏洞扫描
  14. SIFT、SURF、Harris、BRIEF、FAST、DAISY、FAST经典描述子简介
  15. tkmybatis 默认值 问题
  16. kirin710f是什么处理器_kirin710什么处理器
  17. 【科研小技巧】Word(2016)绘制三线表样式
  18. 微信小程序生成Excel
  19. 计算机的基本键盘知识,知识:计算机键盘上每个键的功能_计算机的基本知识_IT /计算机_信息...
  20. 从零开始的种田生活-Unity游戏开发

热门文章

  1. [WPF自定义控件库]自定义Expander
  2. 使用SQL Server Management Studio 创建数据库备份作业
  3. Codeforces Round #237 (Div. 2)
  4. 使用php://input
  5. 使用SPA/GPA 参数--SAP内存参数设置SET /GET PARAMTER ID
  6. Python for和if的连写
  7. pandas 保存csv 不要序号
  8. yolov3从头实现(四)-- darknet53网络tf.keras搭建
  9. python安装sqlalchemy python2_Python SQLAlchemy --2
  10. 人工智能学c语言还是python-python深入学习好还是直接学人工智能好?