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 ...
随机推荐
- linux centos7 修改默认网卡命名规则为eth0脚本
CentOS6之前基于传统的命名方式如:eth1,eth0.... Centos7提供了不同的命名规则,默认是基于固件.拓扑.位置信息来分配.这样做的优点是命名是全自动的.可预知的,缺点是比eth0. ...
- mongodb的索引操作
在mongodb中,当我们一个集合中的数据量非常大时,比如几百万条数据,如果不使用索引,对数据的查询就会进行全表扫描,这个时候查询的速度就会非常的慢,此时我们就需要为集合建立上索引,从而加快查询的速度 ...
- Python网络爬虫实战入门
一.网络爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人),是一种按照一定的规则,自动地抓取万维网信息的程序. 爬虫的基本流程: 发起请求: 通过HTTP库向目标站点发起请求,也就是发送一个Request ...
- Allure快速入门
1.关于Allure Allure框架是一个灵活轻量级多语言测试报告工具,它不仅可以以WEB的方式展示简介的测试结果,而且允许参与开发过程的每个人从日常执行的测试中最大限度的提取有用信息. ...
- PSS--待看
转载:浅谈可移植激励规范(PSS)复用策略_路科验证-CSDN博客 译者按 :当今硬件设计变得愈加复杂,如何创建出足够的测试来保证设计的正确性是每个硬件工程师需要面对的问题.Accellera的可移植 ...
- Luogu P1118 [USACO06FEB]数字三角形 Backward Digit Sums | 搜索、数学
题目链接 思路:设一开始的n个数为a1.a2.a3...an,一步一步合并就可以用a1..an表示出最后剩下来的数,不难发现其中a1..an的系数恰好就是第n层杨辉三角中的数.所以我们可以先处理出第n ...
- Linux Kdump 机制详解
文章目录 1. 简介 1.1 安装 1.2 触发 kdump 1.3 调试 kdump 1.3.1 安装 debuginfo vmlinux 1.3.2 编译 kernel 1.4 kdump-too ...
- C++ substr 的两个用法
substr是C++语言函数,主要功能是复制子字符串,要求从指定位置开始,并具有指定的长度. basic_string substr(size_type _Off = 0,size_type _C ...
- DASCTF 安恒七月赛wp
web Ezfileinclude 首页一张图片,看src就可以看出文件包含 验证了时间戳 尝试用php://filter 读源码读不到,以为只能读.jpg,然后用../路径穿越有waf 最后居然一直 ...
- Java学习(十七)
Java多态的学习差不多有3个小时,老师还夹杂着一些编译器运用的知识. 这是多态的基本知识: 我们可以创建父类引用指向子类对象,这就是多态的一种.(这种也叫向下转型) Pet c=new Cat(); ...