【leetcode】3Sum Closest
3Sum 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(vector<int> &num, int target) { int n=num.size();
sort(num.begin(),num.end()); int i,j,k;
int a,b,c;
int result;
int distance=INT_MAX; for(i=;i<n-;i++)
{
j=i+;
k=n-; while(j<k)
{
a=num[i];
if(i>&&num[i]==num[i-])
{
break;
}
b=num[j];
if(j>i+&&num[j]==num[j-])
{
j++;
continue;
} c=num[k];
if(k<n-&&num[k]==num[k+])
{
k--;
continue;
} if(a+b+c==target)
{
return target;
}
else if(a+b+c<target)
{
if(target-(a+b+c)<distance)
{
result=a+b+c;
distance=target-result;
}
j++;
}
else if(a+b+c>target)
{
if((a+b+c)-target<distance)
{
result=a+b+c;
distance=result-target;
}
k--;
}
}
} return result; }
};
【leetcode】3Sum Closest的更多相关文章
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- 【leetcode】3Sum Closest(middle)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 【LeetCode】3Sum 解决报告
这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...
- 【Leetcode】【Medium】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】3Sum
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- 【leetcode】3Sum (medium)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 【LeetCode】270. Closest Binary Search Tree Value 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- mysql-函数FOUND_ROWS()
FOUND_ROWS() SELECT语句中经常可能用LIMIT限制返回行数.有时候可能想要知道如果没有LIMIT会返回多少行,但又不想再执行一次相同语句.那么,在SELECT查询中包含SQL_CAL ...
- js常用插件
1.jQuery Shortcuts 是个超轻量级的方法,使用 jQuery 来绑定快捷键(热键). 2.Underscore封装了常用的JavaScript对象操作方法,用于提高开发效率. 3.Kn ...
- 未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”
未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” 使用nu ...
- 学习笔记--博弈组合-SG函数
fye学姐的测试唯一的水题.... SG函数是一种游戏图每个节点的评估函数 具体定义为: mex(minimal excludant)是定义在整数集合上的操作.它的自变量是任意整数集合,函数值是不属于 ...
- event driven的一些概念
1. event :Something that happens during your application that requires a response. 2.event object:Th ...
- javaIO(二)
在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据时要使用输入流读取数据,而当程序需要将一些数据保存起来时,就要使用输出流. 在java.io包中流的操作主要有字节流.字符流两大类,两类都 ...
- POJ3267 The Cow Lexicon(DP+删词)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9041 Accepted: 4293 D ...
- apache mysql 正常启动 打开php网页延时
金山毒霸或者升级精灵修改了WINSOCK导致的.由于我电脑上也安装了金山毒霸,而且最近几天也升级过了,应该是同样的问题.于是搜索到恢复Winsock的方法: netsh winsock reset 使 ...
- C++_Eigen函数库用法笔记——Matrix and Vector Arithmetic
Addition and subtraction Scalar multiplication and division Transposition Matrix-matrix and matrix-v ...
- Java NIO原理和使用
Java NIO非堵塞应用通常适用用在I/O读写等方面,我们知道,系统运行的性能瓶颈通常在I/O读写,包括对端口和文件的操作上,过去,在打开一个I/O通道后,read()将一直等待在端口一边读取字节内 ...