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 ...
随机推荐
- Docker(Docker Toolbox)配置镜像加速更换国内源
自己当时装的是Win10专业工作室版本,不知道为什么不支持window for docker, 所以选择了Docker Toolbox 的方式,主要是为了学习,虽然这种方式是不建议安装的,但是基础的学 ...
- 在linux下搭建l2tp隧道
搭一个l2tp隧道,拓扑如下 两台机器是CentOS5,内核选上CONFIG_LEGACY_PTYS选项后自己编译的,l2tp是已经停更的l2tpd-0.69.先在LS上配置IP地址,iptables ...
- 多测师浅谈 学员实现价值就是我们的幸福_高级讲师肖sir
学员实现价值就是我们的幸福 作为一名资深的IT高级讲师,在传统的行业IT薪资基本都是过万,作为一名IT培训教师,培养出在不同领域的测试,并且接触各种各样的产品,目前市场流行的比如银行业务系统,语音类系 ...
- 阅读-Calibre Library转PDF、EPUB配置
提示:如果想恢复默认设置,点击"恢复默认值"即可 -----EPUB (MOBI同理)----- 目标:解决转换过程中图片清晰度丢失问题(分辨率太低) 右击-转换书籍-逐个转换 输 ...
- 最近集训的图论(思路+实现)题目汇总(内容包含tarjan、分层图、拓扑、差分、奇怪的最短路):
(集训模拟赛2)抢掠计划(tarjan强) 题目:给你n个点,m条边的图,每个点有点权,有一些点是"酒吧"点,终点只能在"酒吧",起点给定,路可以重复经过,但点 ...
- centos8平台使用xfs文件系统
一,xfs文件系统的特点 XFS是一种高性能的日志文件系统, 它是由SGI公司设计的,被称为业界最先进的.最具可升级性的文件系统技术. 最初是从unix(irix)移植到linux系统上的. 从cen ...
- LNOI 2020 退役记
不会爆零了吧嘤嘤嘤 \(Day -7\) 周五正在上化学珂,突然被老师叫出去说省选还有名额,问我报不报名.啊嘞嘞还有一周了告诉我还有名额?经过了激烈的思想斗争,还是决定停课搞一搞,学一回OI好歹看看省 ...
- postgresql 导出数据库与数据表
单表导出 pg_dump --host 127.0.0.1 --port 5432 --username "postgres" --role "postgres" ...
- django—模板相关
关于在html文件中使用模板语言时,一些符号的含义 {{}}:显示变量相关,在模板渲染的时候替换成值 {%%}:处理逻辑相关 django模板语言中,对于列表字典等容器类型,不能够通过[ 索引/键值 ...
- 利用ADB命令强制卸载oppo自带浏览器
前言 oppo手机是自带oppo浏览器的,这个自带的浏览器带有oppo推荐的负面新闻很多,而且有时也自动推送一些消息给用户,页面不够简洁,打开浏览器负面内容比较多,所以想要强制卸载oppo浏览器,然后 ...