算法题丨3Sum Closest
描述
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target.
Return the sum of the three integers. You may assume that each input would have exactly one solution.
示例
Given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
算法分析
难度:中
分析:给定整型数组中和一个指定的目标整型值,从数组中找到3个元素,满足3个元素之和最接近目标值,返回结果为3个元素之和。
思路:大体思路类似3Sum算法,也是先将数组排序,然后开始遍历,3个元素分别是当前遍历的元素、夹逼开始元素默认当前元素后面的元素,夹逼结束元素默认数组最后的元素,通过夹逼开始元素递增和夹逼结束元素递减来实现夹逼:
1. 如果这3个元素之和比目标值大,则需要夹逼结束元素值变小,才有可能接近目标值,所以夹逼结束元素递减;
2. 如果这3个元素之和不比目标值大,则需要夹逼开始元素值变大,才有可能接近目标值,所以夹逼开始元素递增;
本次遍历结束后,再判断本次3元素之和目前记录的最接近之和比较,取更接近的做为返回结果,然后继续遍历,直至遍历结果,获得返回结果。
代码示例(C#)
public int ThreeSumClosest(int[] nums, int target)
{
int res = nums[0] + nums[1] + nums[nums.Length - 1];
//排序后遍历
Array.Sort(nums);
for (int i = 0; i < nums.Length - 2; i++)
{
//从当前后面的元素和最后一个元素,两边夹逼
int lo = i + 1, hi = nums.Length - 1;
while (lo < hi)
{
int sum = nums[i] + nums[lo] + nums[hi];
if (sum > target)
{
hi--;
}
else
{
lo++;
}
//如果此次遍历的3个元素的和更接近,则赋值返回的结果
if (Math.Abs(sum - target) < Math.Abs(res - target))
{
res = sum;
}
}
}
return res;
}
复杂度
- 时间复杂度:O (n²).
- 空间复杂度:O (1).
附录
算法题丨3Sum Closest的更多相关文章
- 算法题丨3Sum
描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- 算法题丨Two Sum
描述 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- 算法题丨4Sum
描述 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- 算法题丨Remove Duplicates from Sorted Array II
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...
- 算法题丨Longest Consecutive Sequence
描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- 算法题丨Remove Element
描述 Given an array and a value, remove all instances of that value in-place and return the new length ...
- 算法题丨Move Zeroes
描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...
- 算法题丨Next Permutation
描述 Implement next permutation, which rearranges numbers into the lexicographically next greater perm ...
- 算法题丨Remove Duplicates from Sorted Array
描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...
随机推荐
- angular路由详解一(基础知识)
本人原来是iOS开发,没想到工作后,离iOS开发原来越远,走上了前端的坑.一路走来,也没有向别人一样遇到一个技术上的师傅,无奈只能一个人苦苦摸索.如今又开始填angular的坑了.闲话不扯了.(本人学 ...
- token的时限多长才合适?
在使用JWT时,一个让人纠结的问题就是"Token的时限多长才合适?".对此,Stormpath的这篇文章给出了一个可供参考的建议: 面对极度敏感的信息,如钱或银行数据,那就根本不 ...
- 2018 年 3 月 iOS架构师 面试总结
序言: 今年2月中下旬因为个人原因,换了一份工作,3月初期间面试了有3,4家,基本都是D轮或者刚刚上市的公司,也有上榜的BAT,也从他们的面试笔试中看到了自己的一些不足,于是就想写出来和大家分享一下, ...
- PAT乙级-1036.跟奥巴马一起编程(15)
题解 题解: 注意"行数是列数的50%(四舍五入)" #include<iostream> using namespace std; int main() { int ...
- 设计模式——观察者模式(C++实现)
#include <iostream> #include <vector> #include <algorithm> #include <iterator&g ...
- 智能合约语言 Solidity 教程系列7 - 以太单位及时间单位
这是Solidity教程系列文章第7篇介绍以太单位及时间单位,系列带你全面深入理解Solidity语言. 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所 ...
- R语言-用户细分
案例:通过使用R语言的聚类算法将用户进行合理的划分,找出对超市贡献度,光临度最高的优质客户,对后期的推广有更深远的影响 1.导入包 library(dplyr) library(reshape2) l ...
- SpringBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- Redis单例、主从模式、sentinel以及集群的配置方式及优缺点对比
https://my.oschina.net/zhangxufeng/blog/905611
- Webpack的加载器
一.什么是加载器(loaders)loaders 用于转换应用程序的资源文件,他们是运行在nodejs下的函数 使用参数来获取一个资源的来源并且返回一个新的来源(资源的位置),例如:你可以使用load ...