leetcode133:3sum-closest
题目描述
例如,给定的整数 S = {-1 2 1 -4}, 目标值 = 1.↵↵ 最接近目标值的和为 2. (-1 + 2 + 1 = 2).
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).
输出
0
链接:https://www.nowcoder.com/questionTerminal/291a866d7c904563876b373b0b157dde?f=discussion
来源:牛客网
class
Solution {
public
:
int
threeSumClosest(vector<
int
> &num,
int
target) {
int
n = num.size();
int
result = num[0] + num[1] + num[n-1];
sort(num.begin(),num.end());
for
(
int
i=0;i<n-2;i++)
{
int
start = i + 1;
int
end = n - 1;
while
(start < end)
{
int
sum = num[i] + num[start] + num[end];
if
(sum < target)
start++;
else
end--;
if
(
abs
(sum-target) <
abs
(result-target))
result = sum;
}
}
return
result;
}
};
leetcode133:3sum-closest的更多相关文章
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 2Sum,3Sum,4Sum,kSum,3Sum Closest系列
1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- LeetCode--No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- leetcode-algorithms-16 3Sum Closest
leetcode-algorithms-16 3Sum Closest Given an array nums of n integers and an integer target, find th ...
随机推荐
- matlab中floor 朝负无穷大四舍五入
来源:https://ww2.mathworks.cn/help/matlab/ref/floor.html?searchHighlight=floor&s_tid=doc_srchtitle ...
- 为Android(和其他移动平台)安装MoSync
为Android(和其他移动平台)安装MoSync Android教程比赛 这是我提交的文章#2:设置你的Android开发环境.它的主要区别在于它描述了如何安装MoSync,这是一种开发环境,它不是 ...
- operator bool()是什么
operator bool()是什么 在C++中,operator TypeName()语法用来将对象转换为指定的TypeName类型,当这里TypeName为bool时,就可以直接在条件判断式里面直 ...
- C++里面类和对象是什么意思?
本文章向大家介绍C++类和对象到底是什么意思?,主要包括C++类和对象到底是什么意思?使用实例.应用技巧.基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下. C++ 是一门 ...
- 飞翔---------双重线性dp
题目: 鹰最骄傲的就是翱翔,但是鹰们互相都很嫉妒别的鹰比自己飞的快,更嫉妒其他的鹰比自己飞行的有技巧.于是,他们决定举办一场比赛,比赛的地方将在一个迷宫之中. 这些鹰的起始点被设在一个N*M矩阵的左下 ...
- spring boot:解决cors跨域问题的两种方法(spring boot 2.3.2)
一,什么是CORS? 1,CORS(跨域资源共享)(CORS,Cross-origin resource sharing), 它是一个 W3C 标准中浏览器技术的规范, 它允许浏览器向非同一个域的服务 ...
- arcgis activeX 安全提示消除办法
点击任何的一个ArcToolBox 工具,光标落在参数输入框时,会提示 "在此页面上的ActiveX控件和本页上的其他部分的交互可能不安全.你想允许这种交互操作吗? " 消除办法 ...
- 第二个 SignalR,可以私聊的聊天室
一.简介 上一次,我们写了个简单的聊天室,接下来,我们来整一个可以私聊的聊天室. SignalR 官方 API 文档 需求简单分析: 1.私聊功能,那么要记录用户名或用户ID,用于发送消息. 2.怎么 ...
- 2020年Java基础超高频面试题汇总(1.2W字详细解析)
1. Java语言有哪些特点 (1)简单易学.有丰富的类库 (2)面向对象(Java最重要的特性,让程序耦合度更低,内聚性更高) (3)与平台无关性(JVM是Java跨平台使用的根本) (4)可靠安全 ...
- bzoj2539 丘比特的烦恼、黑书P333 (最优二分图匹配)
丘比特的烦恼 题目描述 Description 随着社会的不断发展,人与人之间的感情越来越功利化.最近,爱神丘比特发现,爱情也已不再是完全纯洁的了.这使得丘比特很是苦恼,他越来越难找到合适的男女, ...