LeetCode之16----3Sums 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.
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).
题目大意:
思路:
代码:
class Solution {
public:
int threeSumClosest(std::vector<int>& nums, int target) {
int result = target, dis = INT_MAX, dis_tmp, tmp;
if (nums.size() == 0) {
result = 0;
}
for (int i = 0; i < nums.size(); ++i) {
if (i != 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < nums.size(); ++j) {
if (j != i + 1 && nums[j] == nums[j - 1]) {
continue;
}
for (int k = j + 1; k < nums.size(); ++k) {
if (k != j + 1 && nums[k] == nums[k - 1]) {
continue;
}
tmp = nums[i] + nums[j] + nums[k];
dis_tmp = abs(tmp - target);
if (dis_tmp < dis) {
dis = dis_tmp;
result = tmp;
if (result == target) {
return target;
}
}
}
}
}
return result;
}
};
3Sums改进法:
class Solution {
public:
int threeSumClosest(std::vector<int>& nums, int target) {
const int n = nums.size();
sort(nums.begin(),nums.end());
//假设最大的三个数加起来还小于目标值,则最接近的就是这三个数相加
if(nums[n-1] + nums[n-2] + nums[n-3] <= target) {
return nums[n-1] + nums[n-2] + nums[n-3];
}
//假设最小的三个数加起来还大于目标值,则最接近的就是这三个数相加
if(nums[0] + nums[1] + nums[2] >= target) {
return nums[0] + nums[1] + nums[2];
}
int tmp; //候选数
int dis = INT_MAX; //距离
//由于要选择三个数,所以i < n - 2
for (int i = 0; i < n-2; i++) {
//假设当前处理过的数字上一次处理过,则不再处理
if (i != 0 && nums[i] == nums[i-1]) {
continue;
}
//假设当前扫描的值加上最大的三个数字之后假设还小于目标值
//说明和目标值相等的概率为零(即:差值为0的概率为0)
if (nums[i] + nums[n-1] + nums[n-2] <= target) {
tmp = nums[i] + nums[n-1] + nums[n-2];
if (tmp == target) {
return target;
}
dis = tmp - target; //差值最小等于候选值减去目标值
continue;
}
//假设和最大的三个数字相加之后大于目标值,说明还有希望找到差值为0的值
//接下来就是求2Sums问题了(不同的一点是加了一个差值最小的推断)
int target2 = target - nums[i];
int j = i + 1;
int k = n - 1;
while (j < k) {
const int sum2 = nums[j] + nums[k];
if (abs(sum2 - target2) < abs(dis)){
dis = sum2 - target2;
}
if(sum2 > target2) {
k--;
}
else if(sum2 < target2) {
j++;
}
else {
return target;
}
while (nums[j] == nums[j-1]) {
j++;
}
while(nums[k] == nums[k+1]) {
k--;
}
}
}
return target + dis;
}
};
LeetCode之16----3Sums Closest的更多相关文章
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【一天一道LeetCode】#16. 3Sum Closest
一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- 【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 ...
- LeetCode:16. 3Sum Closest(Medium)
1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...
- Leetcode Array 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(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- [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 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
随机推荐
- iOS学习笔记05-触摸事件
一.事件分发处理[由外到内] 在iOS中发生触摸后,事件会加到UIApplication事件队列,UIApplication会从事件队列取出最前面的事件进行分发处理,通常会先分发给主窗口,主窗口会调用 ...
- Kubernetes对象
Kubernetes对象 在之前的文章已经讲到了很多Kubernets对象,包括pod,service,deployment等等.Kubernets对象是一种持久化,表示集群状态的实体.它是一种声明式 ...
- mybatis学习(一)——mybatis简介
1.简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBati ...
- VS2015 “GENERATERESOURCE”任务意外失败 解决方法
昨天把项目解决方案Copy到另外的机器上执行,遭遇了一场"任务意外失败",网上搜索一下,顺利解决了,在此记录一下. Visual Studio.net 工程更换机器编译时遇到”Ge ...
- Hubtown
Hubtown 时间限制: 10 Sec 内存限制: 256 MB 题目描述 Hubtown is a large Nordic city which is home to n citizens. ...
- EC++学习笔记(六) 继承和面向对象设计
条款32:确定你的 public 继承塑模出 is-a 关系 public inheritance 意味着 is-a 关系class Derived 以 public 形式继承 class Base, ...
- [NOIP2011] 洛谷P1313 计算系数
题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别为 a ,b ,k , ...
- iOS常用三方库收集
除非Pod可以直接加载到工程中的外,收集一下 https://github.com/kejinlu/KKGestureLockView 好用的手势解锁
- AC日记——美元汇率 洛谷 P1988
题目背景 此处省略maxint+1个数 题目描述 在以后的若干天里戴维将学习美元与德国马克的汇率.编写程序帮助戴维何时应买或卖马克或美元,使他从100美元开始,最后能获得最高可能的价值. 输入输出格式 ...
- (5)ASP.NET Core 中的静态文件
1.前言 当我们创建Core项目的时候,Web根目录下会有个wwwroot文件目录,wwwroot文件目录里面默认有HTML.CSS.IMG.JavaScript等文件,而这些文件都是Core提供给客 ...