40-3Sum Closest
3Sum Closest My Submissions QuestionEditorial Solution
Total Accepted: 76185 Total Submissions: 262100 Difficulty: Medium
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).
Submission Details
120 / 120 test cases passed.
Status: Accepted
Runtime: 24 ms
beats:25.39%
思路:参看我写的3sum,4sum,思路差不多,
先固定一个数,剩下的两数进行左右夹逼,跳过不必要的判断,
用best_sum来记录距离最近的和,用error记录距离target的距离
#define IMAX numeric_limits<int>::max()
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
size_t n = nums.size();
sort(nums.begin(),nums.end());
int best_sum=0,error=IMAX;
for(int i=0;i<n;++i){
int beg = i+1,end = n-1;
while(beg<end){
int tsum =nums[i]+nums[beg]+nums[end];
if(tsum==target) {
best_sum = tsum;
while(++beg<end&&nums[beg]==nums[beg-1]);//如果和前一个数一样跳过
while(--end>beg&&nums[end]==nums[end]);//如果和后一个数一样,跳过
}
else{
if(tsum<target)beg++;
else end--;
}
if(abs(target-tsum)<error){
error = abs(target-tsum);
best_sum = tsum;
}
}
}
return best_sum;
}
};
40-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 ...
随机推荐
- [no code][scrum meeting] Alpha 15
项目 内容 会议时间 2020-04-23 会议主题 OCR紧急会议 会议时长 45min 参会人员 PM + OCR组(赵涛,黎正宇) 项目 内容 会议时间 2020-04-24 会议主题 全体测试 ...
- MySQL实战优化之InnoDB整体架构
一.InnoDB 更新数据得整体架构 每个组件的作用说明: 用一条更新数据来说明每个主键得作用: update student set name = 'zhangsan' where id = 10 ...
- 我为啥开始用CSDN博客
今晚开通CSDN博客,并且决定以后每天都使用这个不错的东西.与此同时,在博客园也开通了一个:http://www.cnblogs.com/fish7/ 我原本是把做过的题都用WPS整理的,然后每次打印 ...
- 一步一步学ROP之linux_x86篇(蒸米spark)
目录 一步一步学ROP之linux_x86篇(蒸米spark) 0x00 序 0x01 Control Flow Hijack 程序流劫持 0x02 Ret2libc – Bypass DEP 通过r ...
- openstack 虚机热迁移问题:虚机状态一直处于迁移中的情况处理
前提:在偶尔的虚机热迁移中,发现虚机一直属于迁移状态中. 但是查看后台流量监控,发现没有流量已经下来了.然后在目标机器上查看,发现kvm已经在目标机器上. 1.查看kvm 实际所处宿主机方法: a.拿 ...
- SpringCloud微服务实战——搭建企业级开发框架(十五):集成Sentinel高可用流量管理框架【熔断降级】
Sentinel除了流量控制以外,对调用链路中不稳定的资源进行熔断降级也是保障高可用的重要措施之一.由于调用关系的复杂性,如果调用链路中的某个资源不稳定,最终会导致请求发生堆积.Sentinel ...
- Spark记录(一):Spark全景概述
一.Spark是什么 Spark是一个开源的大数据处理引擎. 二.Spark的主要组件如下图所示: 三.Spark运行时架构 Spark共有三种运行模式:本地模式.集群模式.客户端模式. 生产环境基 ...
- properties 文件解析
1.提供properties文件 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/future?useUn ...
- 完美解决Github网页打开超慢的问题
由于某些原因,国内访问Github会异常缓慢,在clone仓库时甚至只有10k以下的速度,下载半天有时还会失败需要从头再来,甚是让人恼火.本文介绍通过修改系统hosts文件的办法,绕过国内dns解析, ...
- 『学了就忘』Linux基础命令 — 35、网络中与其他机器通信的命令
目录 1.write命令 2.wall命令 3.mail 命令 使用1:发送邮件 使用2:查看已经接收的邮件 使用3:发送文件内容 1.write命令 (1)write命令的基本信息 命令名称:wri ...