LeetCode——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/
二.题目大意:
给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的和最接近target。
三.题解:
这道题实质就是3sum(http://www.cnblogs.com/wangkundentisy/p/9079622.html)问题的变形,而且题目假设的是解是唯一的,所以该题甚至都不用考虑重复情况了。所以只需在3sum问题中,判断下三个元素的和与target的差值,并根据差值进行赋值即可。
代码如下:
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int len = nums.size();
int differ = 0x7fffffff;//初始化差值
int sum = 0;
if(len < 3)
return 0;
sort(nums.begin(),nums.end());
for(int i = 0; i < len; i++)
{
if(i != 0 && nums[i] == nums[i -1])
continue;
int j = i + 1, k = len - 1;
while(j < k)
{
if(nums[i]+ nums[j] + nums[k] == target)
{
return target;//由于题目假设的是解释唯一的,所以如果遇到正好和为target的情况,可以立马返回
}
else if(nums[i] + nums[j] + nums[k] < target)
{
int temp = target - (nums[i] + nums[j] + nums[k]);
if(temp < differ)//找到离target差异最小的一组数据
{
sum = nums[i] + nums[j] + nums[k];
differ = temp;
}
j++;
}
else
{
int temp = (nums[i] + nums[j] + nums[k]) - target;
if(temp < differ)
{
sum = nums[i] + nums[j] + nums[k];
differ = temp;
}
k--;
}
}
}
return sum;
}
};
由于本题基本与3sum问题一致,所以注意事项直接看3sum问题就行了。
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 [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 ...
随机推荐
- Unity打包/读取AssetBundle资源全教程
Unity 资源AssetBundle打包 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar ...
- 改变html中的内容
$("#id").html() 获取内容 $("#id").html(xiugai) 修改内容
- i = i ++;的分解
步骤分解: package wrong; public class ShortTest { public static void main(String args[]) { /* * i = i ++ ...
- PTA——类型转换
PTA习题 7-6 厘米换算英尺英寸 (15 分) #include<stdio.h> int main(){ int a; int b,c; scanf("%d",& ...
- 计算x
如果x的x次幂结果为10(参见[图1.png]),你能计算出x的近似值吗? 显然,这个值是介于2和3之间的一个数字. 请把x的值计算到小数后6位(四舍五入),并填写这个小数值. 注意:只填写一个小数, ...
- C# 敏捷1
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; ...
- oracle 日常
oracle sql server select 1 from dual 和 select 1 nvl(null,0) 为空显示 0 greatest (1, 3, 2 ) ...
- MySQL5.6在线DDL不锁表(在线添加字段)
解答你也看一下MySQL5.6在线DDL不锁表,现在我有一张1亿的表,需要增加一个字段,假如我让你去增加这个字段,你应该注意什么,具体怎么操作? 操作如下:1.注意磁盘空间(临时表目录 参数 tmpd ...
- Java开发培训基础知识解析之反射机制
Java是老牌编程语言,是当前应用最广泛的编程语言之一.想要学习Java你就一定要掌握Java基础知识,而反射对于初学Java的人来说绝对是非常重要的知识点.什么是反射?如何理解反射机制?如何使用反射 ...
- streamdataio 实时数据分发平台
streamdataio 是一个实时的数据分发平台(当然是收费的,但是设计部分可以借鉴),我们可以通过这个平台 方便的拉取rest api 数据,或者发布数据到后端,streamdataio 可以帮助 ...