问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3620 访问。

给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.

与 target 最接近的三个数的和为 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.

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3620 访问。

public class Program {

    public static void Main(string[] args) {
var intervals = new int[] { -1, 2, 1, -4 };
var target = 1; var res = ThreeSumClosest(intervals, target);
Console.WriteLine(res); Console.ReadKey();
} public static int ThreeSumClosest(int[] nums, int target) {
//基础思路同“3数之和”
Array.Sort(nums);
//定义结果、3数之和
var res = 0;
var sum = 0;
//定义上一次和这一次的差
var prediff = int.MaxValue;
var diff = 0;
//定义左右双指针
var j = 0;
var k = 0;
//双指针 j、k 循环分析
for(var i = 0; i < nums.Length - 2; i++) {
j = i + 1;
k = nums.Length - 1;
//左指针不能大于或等于右指针
while(j < k) {
sum = nums[i] + nums[j] + nums[k];
//3数之和与目标值比
//根据结果移动左右指针或相等时直接返回结果
if(sum > target) k--;
else if(sum < target) j++;
else return sum;
//计算差值
diff = Math.Abs(sum - target);
//如果本次更小,重置结果和“上一次差值”
if(diff < prediff) {
res = sum;
prediff = diff;
}
}
}
//返回结果
return res;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3620 访问。

2

分析

显而易见, 以上算法的时间复杂度为:O(n2)O(n^2)O(n2) 。

C#LeetCode刷题之#16-最接近的三数之和(3Sum Closest)的更多相关文章

  1. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  2. Leetcode题库——16.最接近的三数之和

    @author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...

  3. [Swift]LeetCode16. 最接近的三数之和 | 3Sum Closest

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  4. Java实现 LeetCode 16 最接近的三数之和

    16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...

  5. leetcode.数组.16最接近的三数之和-java

    1. 具体题目 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案 ...

  6. [LeetCode] 16. 最接近的三数之和

    题目链接:https://leetcode-cn.com/problems/3sum-closest/ 题目描述: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 num ...

  7. LeetCode 16. 最接近的三数之和(3Sum Closest)

    题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例 ...

  8. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  9. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  10. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

随机推荐

  1. python---Flask使用教程-加载静态文件

    flask的静态文件,一般放在static目录下,前端页面放在templates下(而且这两个名字是定死的(static,templates)),目录结构如图: 模板(index.html)里加载静态 ...

  2. SpingBoot整合jxls2.0-excel导出—— 列表循环,自定义方法,超链接等

    Java中实现excel导出数据的方法有很多,一般简单的可以通过操作POI进行,但是复杂的excel格式导出如果用POI就显得非常麻烦,本文介绍的jxls2.0完全依据模板进行导出,只需要进行简单的配 ...

  3. mysql数据delete后的数据恢复

    误删除了一个订单,订单号为:D200711008, 现根据binlog对该订单数据进行恢复. 1. 查看binlog日志, 取最新的一个 mysql-bin.000635 show binary lo ...

  4. WYT的刷子

    WYT的刷子 题目描述 WYT有一把巨大的刷子,刷子的宽度为M米,现在WYT要使用这把大刷子去粉刷有N列的栅栏(每列宽度都为1米:每列的高度单位也为米,由输入数据给出). 使用刷子的规则是: 与地面垂 ...

  5. SQL : 把特定的数据排前面 & 分别查询几组数据的最大值

    把特定的数据排前面 : 比如说,把没有审核身份证的人排最前面,然后再按userId正序排. select case when idcardverified = 1 then 0 else 1 end ...

  6. Java中的锁机制

    1.在Java中锁的分类 其实就是按照锁的特性分类的 公平锁,非公平锁 可重入锁 独享锁,共享锁 互斥锁,读写锁 乐观锁,悲观锁 分段锁 偏向锁,轻量级锁,重量级锁 自旋锁 相关资料:思维导图 使用场 ...

  7. Android:沉浸式状态栏(二)集成

    在Activity的onCreate()方法中添加代码 //设置状态栏透明 StatusBarUtil.setTranslucentStatus(this); //设置透明状态栏的paddingTop ...

  8. 关于RecyclerView(二)设置EmptyView

    首先重写一个RecyclerView类 package com.onepilltest.others; import android.content.Context; import android.s ...

  9. MacOS下VUEJS简单入门

    微信搜索"艺术行者",关注并回复关键词"vue"获取课程资料 上传的在线学习视频(黑马和传智双元,感谢) VueJs概述与快速入门 学习之前假设你已了解关于 H ...

  10. Oracle 忘记密码 如何修改

    原文链接:https://jingyan.baidu.com/article/358570f6aaa1efce4724fcdf.html ️打开运行窗口 ️输入sqlplus "/ as s ...