[array] leetCode-16. 3Sum Closest -Medium
16. 3Sum Closest -Medium
descrition
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).
解析
与 3Sum 的思路一样。不同在于,我们现在希望找到距离 target 最近的数,参看代码。
code
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
using namespace std;
class Solution{
public:
int threeSumClosest(vector<int>& nums, int target){
sort(nums.begin(), nums.end()); // ascending
int min_gab = numeric_limits<int>::max();
int ans = target;
for(int i=0; i<nums.size(); i++){
int target_local = target - nums[i];
int ileft = i + 1;
int iright = nums.size() - 1;
while(ileft < iright){ // two pointer searching
int sum = nums[ileft] + nums[iright];
if(sum == target_local) // right answer
return target;
if(sum < target_local) // move ileft to increase sum
ileft++;
else // sum > target_local
iright--;
int gab = abs(sum - target_local);
if(gab < min_gab){
ans = sum + nums[i];
min_gab = gab;
}
}
}
return ans;
}
};
int main()
{
return 0;
}
[array] leetCode-16. 3Sum Closest -Medium的更多相关文章
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- 蜗牛慢慢爬 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 ...
- Array + two points leetcode.16 - 3Sum Closest
题面 Given an array nums of n integers and an integer target, find three integers in nums such that th ...
- [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 ☆☆☆
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
随机推荐
- HDU 6153 A Secret
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total ...
- hello world! hello cnbog
第一次开通博客,以后见证我的成长吧!
- 【习题 8-11 UVA - 1615】Highway
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个村庄都有一个范围[li..ri] 只要在这个范围内放点都可以"支配"这个村庄. 则问题就等价于线段上有n个区 ...
- 【hdu 4289】Control
[Link]:http://acm.hdu.edu.cn/showproblem.php?pid=4289 [Description] 给出一个又n个点,m条边组成的无向图.给出两个点s,t.对于图中 ...
- 洛谷——P2678 跳石头
https://www.luogu.org/problem/show?pid=2678#sub 题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着 ...
- POJ 1426 Find The Multiple (DFS / BFS)
题目链接:id=1426">Find The Multiple 解析:直接从前往后搜.设当前数为k用long long保存,则下一个数不是k*10就是k*10+1 AC代码: /* D ...
- inflater-布局转化实现
通俗的说,inflate就相当于将一个xml中定义的布局找出来.因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件 ...
- listview-fading 滚动条样式设置
fadingEdge-属性用来设置拉滚动条时 ,边框渐变的方向.它有三个属性值可以设置 none:(边框颜色不变) horizontal:(水平方向颜色变淡) vertical:(垂直方向颜色变淡). ...
- worktools-源码下拉问题
今天下拉源码的时候,出现了一个问题,就是当地的内容跟仓库的内容冲突,导致merge冲突.这时候CC指令不能用.然后希望通过checkout到其他分支,然后cc掉的.结果没办法切换到其他分支,一直停留在 ...
- 44.delete用法
声明+delete:函数禁止使用.可以使一个类禁止释放