【一天一道LeetCode】#16. 3Sum Closest
一天一道LeetCode系列
(一)题目:
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.
For example, given array S = {-1 2 1 -4}, and target = 1. The sum that
is closest to the target is 2. (-1 + 2 + 1 = 2).
(二)解题
直接用三重循环,然后考虑到重复的数字,则需要先排序,以便于后续去重。
其次,当等于target时,则直接返回
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int min = 2147438647;
int key =0;
std::sort(nums.begin() , nums.end());
for(int i = 0 ; i < nums.size()-2 ; )
{
for(int j = i+1 ; j < nums.size()-1 ;)
{
for(int k = j+1 ; k < nums.size() ; )
{
int gap = nums[i]+nums[j]+nums[k];
int temp = gap-target>0?gap-target:target-gap;
if(temp<min){
min = temp;
key = gap;
if(min ==0) //如果找到等于0的则返回
{
return key;
}
}
k++;
while(k<nums.size() && nums[k] == nums[k-1]) ++k;
}
j++;
while(j<nums.size()-1 && nums[j] == nums[j-1]) ++j;
}
i++;
while(i<nums.size()-2 && nums[i] == nums[i-1]) ++i;
}
return key;
}
};
在网上看到另外一种快速的解法。O(n^2)
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
std::sort(nums.begin() , nums.end());
bool isfirst = true;
int ret;
for(int i = 0 ; i < nums.size() ; i++)
{
int j = i+1;
int k = nums.size()-1;
while(j<k){
int sum = nums[i]+nums[j]+nums[k];
if(isfirst)
{
ret = sum;
isfirst = false;
}
else
{
if(abs(sum - target) < abs(ret - target))
{
ret = sum;
}
}
if(ret == target)
return ret;
if(sum>target)
k--;
else
j++;
}
}
return ret;
}
};
【一天一道LeetCode】#16. 3Sum Closest的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- Leetcode 16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- Java [leetcode 16] 3Sum Closest
题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...
- [LeetCode] 16. 3Sum Closest 解题思路
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode 16. 3Sum Closest. (最接近的三数之和)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...
- 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- [LeetCode] 16. 3Sum Closest ☆☆☆
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
随机推荐
- 利用QrCode.Net生成二维码 asp.net mvc c#
利用QrCode.Net生成二维码 asp.net mvc c# 里面介绍了.net的方式及js的方式,还不错. 里面用到的qrcode.net的类库下载地址:https://qrcodenet.co ...
- iOS 中捕获截屏操作
转自:iOS知识小集 在iOS 7后,苹果提供了UIApplicationUserDidTakeScreenshotNotification通知来告诉App用户做了截屏操作.苹果的描述如下: // T ...
- 递归dict
一个看起来非常酷的定义 class Example(dict): def __getitem__(self, item): try: return dict.__getitem__(self, ite ...
- Dynamics CRM FORM脚本库加载本地脚本
用传统的开发方式,js脚本都是保存在数据库中的,这样也方便迁移,但如果不想存数据库而是存在物理磁盘上,则可通过下述代码,将脚本存放在CRMWEB文件夹的某个路径下,每次都动态引用本地JS. funct ...
- JAVA面向对象-----super关键字
JAVA面向对象-–super关键字 1:定义Father(父类)类 1:成员变量int x=1; 2:构造方法无参的和有参的,有输出语句 2:定义Son类extends Father类 1:成员变量 ...
- XML之SAX解析模型
DOM解析会把整个XML文件全部映射成Document里的树形结构,当遇到比较大的文件时,它的内存占用很大,查找很慢 SAX就是针对这种情况出现的解决方案,SAX解析器会从XML文件的起始位置起进行解 ...
- EBS总账(GL)模块常用表
select * from gl_sets_of_books 总帐 select * from gl_code_combinations gcc wheregcc.summary_flag='Y ...
- Emojicon表情之快速应用于Android项目
最近在项目中遇到了一个问题,找了半天原因,最后发现是用户在昵称中输入了emojicon表情,导致服务器不识别出现错误,而项目中也未对emojicon表情作支持,因此不得不考虑对emojicon表情做下 ...
- (Android自定义View)来来来,一起再撸一个Material风格loadingView。
本文同步自博主的个人博客wing的地方酒馆 很久很久以前,撸了一款loadingview(点击这里回顾),当时觉得还不错,现在看看觉得好丑啊!!! 于是想再撸一个,无意间在这里看到一个很不错的效果,于 ...
- 关于Python编程的一些问答
关于Python编程的一些问答 导语 大约1个月前,oschina.net和华章图书一起合作做了一个活动:OSC第51期高手问答--聊聊python那些事,来推广我参与撰写的书<编写高质量代码: ...