leetcode第16题--3Sum Closest
Problem: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).
吃饭之前刷了这题。思路就是和上题类似。先srot,然后固定i从第一个到倒数第三个,然后设一个left=i+1,right=num.size()-1; 如果三个数相加和target相比较大,那就right--,使三个数相加要变小。如果比target小那就left++。同时用abs记录当前的差值,如果比之前的差值更小,那就记录三个数的和到val中。如果差值为零了,那就直接返回三个数的和。否则到最后在返回val就可以了。代码如下
class Solution {
public:
int threeSumClosest(vector<int> &num, int target)
{
int left = , right = , val = , minC = INT_MAX;
sort(num.begin(), num.end());
for (int i = ; i < num.size() - ; ++i)
{
left = i + ; right = num.size() - ;
while(left < right)
{
int tepVal = target - num[i] - num[left] - num[right];
if (abs(tepVal) < minC)
{minC = abs(tepVal); val = num[i] + num[left] + num[right];}
if (tepVal > )
left++;
else if (tepVal < )
right--;
else
return num[i] + num[left] + num[right];
}
}
return val;
}
};
吃饭去了。
2015/4/3:
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
sort(num.begin(), num.end());
int ans, globalVal = INT_MAX;
for (int i = ; i < num.size(); ++i){
if (i > && num[i] == num[i-])
continue;
int left = i+, right = num.size()-;
while(left < right){
int val =target - (num[i] + num[left] + num[right]);
if (abs(val) < globalVal){
ans = num[i] + num[left] + num[right];
globalVal = abs(val);
}
if (val == )
return target;
else if(val > )
left++;
else
right--;
}
}
return ans;
}
};
leetcode第16题--3Sum Closest的更多相关文章
- 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]题(Java):3Sum Closest 标签:Array
题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...
- LeetCode第[16]题(Java):3Sum Closest (和目标值最接近的三个数的和)——Medium
题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...
- LeetCode(16)题解--3Sum Closest
https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers ...
- [算法题] 3Sum Closest
题目内容 Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- 【LeetCode OJ 016】3Sum Closest
题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integ ...
- leetcode第15题--3Sum
Problem: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fi ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
随机推荐
- Team Foundation Server 2015使用教程--默认团队权限说明
- Ubuntu文件的复制、移动和删除命令
先说说cp复制命令 该命令的功能是将给出的文件或文件夹复制到还有一文件或文件夹中,同MSDOS下的copy命令一样,功能十分强大. 语法: cp [选项] 源文件或文件夹 目标文件或文件夹 说明:该命 ...
- 介绍 Microservice
介绍 Microservice 这篇文章转自我的 Github blog 一天我司招财猫姐(HR 大人)问我,你给我解释一下 Microservice 是什么吧.故成此文.一切都是从一个创业公司开始的 ...
- 机器人操作系统 除了Android还有一个ROS(转)
你知道市面上的机器人都采用了哪些操作系统吗? 估计大多数人给出的答案就是 Android 了.从市面上的产品来看,基于 Android 系统开发的机器人确实是主流,但是还有一种操作系统却鲜为人知,它叫 ...
- JavaEE(16) - JPA生命周期及监听器
1. 理解实体的生命周期 2. 为实体生命周期事件定义监听器 3. 通过监听实现回调 4. 排除默认监听器和父类上定义的监听器 1. 理解实体的生命周期(Net Beans创建Java Project ...
- Nlog 配置总结
Writes log messages to one or more files. Since NLog 4.3 the ${basedir} isn't needed anymore for rel ...
- 浅谈数据结构之KMP(串中的模式匹配算法)
KMP算法是一种模式匹配算法的改进版,其通过减少匹配的次数以及使主串不回朔来减少字符串匹配的次数,从而较少算法的相应代价,但是,事件万物是普遍归中的,KMP算法的有效性也是有一定的局限的,我将在本文的 ...
- 网络资源(5) - Android视频
2014_08_24 http://v.youku.com/v_show/id_XMjM5NjU2OTI0.html?f=5486194 Android开发视频教程1 http://v.youku.c ...
- android download学习记录
东西拼凑,最终弄出来能够用的代码 [1].[代码] [Java]代码 跳至 [1] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- java_eclipse_svn 与服务器同步时 ,忽略某类型文件和文件夹
1. 在项目开发中使用svn ,带来很大的方便,有时我们会把整个项目上传的svn服务器上 这样就包含了 编译过的class文件 以及 一些 .svn,.log文件,有些文件时本地complie 的 ...