C#LeetCode刷题之#16-最接近的三数之和(3Sum Closest)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- #leetcode刷题之路16-最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- Leetcode题库——16.最接近的三数之和
@author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...
- [Swift]LeetCode16. 最接近的三数之和 | 3Sum Closest
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- leetcode.数组.16最接近的三数之和-java
1. 具体题目 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案 ...
- [LeetCode] 16. 最接近的三数之和
题目链接:https://leetcode-cn.com/problems/3sum-closest/ 题目描述: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 num ...
- LeetCode 16. 最接近的三数之和(3Sum Closest)
题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例 ...
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
随机推荐
- Event-Driven Architecture思考
什么是Event? An event represents a fact, something happened; and it is immutab. 事件代表着事实,代表着过去发生的某件事情,是不 ...
- 老司机带你玩转面试(4):Redis 高可用之哨兵模式
前文回顾 建议前面文章没看过的同学先看下前面的文章: 「老司机带你玩转面试(1):缓存中间件 Redis 基础知识以及数据持久化」 「老司机带你玩转面试(2):Redis 过期策略以及缓存雪崩.击穿. ...
- ArcGIS api for javascript querytask 返回结果上限1000的问题
我用的是ArcServer 10.2,打开ArcCatalog找到自己发布的服务,右键Service Properties,左侧点击Parameters,右侧有一个Maximum number of ...
- js:数组(创建、遍历、函数)
1.数组 采用单个变量只能存储一个数据,数组能够存储多个数据,获取方式也比较简单.它是将一组数据存储在当个变量下的存储方式. 2.数组的创建 (1)new方式创建,不指定数组长度 <script ...
- IO、NIO实现简单聊天室,附带问题解析
本篇文章主要使用IO和NIO的形式来实现一个简单的聊天室,并且说明IO方法存在的问题,而NIO又是如何解决的. 大概的框架为,先提供思路和大概框架图--代码--问题及解决方式,这样会容易看一点 ...
- 分布式ID生成服务,真的有必要搞一个
目录 阐述背景 Leaf snowflake 模式介绍 Leaf segment 模式介绍 Leaf 改造支持RPC 阐述背景 不吹嘘,不夸张,项目中用到ID生成的场景确实挺多.比如业务要做幂等的时候 ...
- .net core 自带分布式事务的微服务开源框架JMS
事务的统一性是微服务的一个重点问题,简洁有效的控制事务,更是程序员所需要的.JMS的诞生,就是为了更简单.更有效的控制事务. 先看一段调用微服务的代码: using (var ms = new JMS ...
- SpringBoot+Vue项目上手
博客 https://gitee.com/RoadsideParty/White-Jotter-Vue?_from=gitee_search UI框架 https://at-ui.github.io/ ...
- 关于RecyclerView(二)设置EmptyView
首先重写一个RecyclerView类 package com.onepilltest.others; import android.content.Context; import android.s ...
- LFM oversea投资笔记、思摩尔、移卡、建业新生活、鑫苑物业
LFM oversea 1.公司简介 2.业务展望 3. 投资策略 LFM oversea介绍 LFM oversea是在开曼成立的投资基金, 专注于中国优质企业在香港和美国IPO过程中的 基石.锚定 ...