前言
本文介绍了 LeetCode 第 16 题 , “3Sum Closest”, 也就是 “最接近的三数之和” 的问题.
本文使用 C# 语言完成题目,介绍了1种方法供大家参考。
题目
English
LeetCode 16. 3Sum Closest
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).
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.
Th

本文详细介绍了LeetCode第16题——最接近的三数之和的问题,提供了一种C#解决方案。通过双指针法找到数组中和最接近目标值的三个数,重点在于更新最接近差值的过程。代码执行时间为104ms,空间复杂度为O(1)。
最低0.47元/天 解锁文章
276

被折叠的 条评论
为什么被折叠?



