1. 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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  4. 【leetcode】3Sum Closest

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  5. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列

    1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...

  6. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  7. 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 ...

  8. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  9. LeetCode--No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  10. leetcode-algorithms-16 3Sum Closest

    leetcode-algorithms-16 3Sum Closest Given an array nums of n integers and an integer target, find th ...

随机推荐

  1. 【二食堂】Alpha - Scrum Meeting 8

    Scrum Meeting 8 例会时间:4.18 11:40 - 12:10 进度情况 组员 昨日进度 今日任务 李健 1. 实体的添加和关系的添加实现的有bug,柴博和刘阳进行了帮助issue 1 ...

  2. Noip模拟72 2021.10.9

    T1 出了个大阴间题 真就以为他出了个大阴间题就没写,打个暴力就跑了 数据范围显然摆明是状压 设$f[sta][0/1]$表示在已经选择的集合$sta$中,$A$的最大值是$A$还是$A+1$ 然后按 ...

  3. 海思 core 电压动态调整

    http://www.eda365.com/forum.php?mod=viewthread&tid=108620&_dsign=5bee4dcb http://www.eda365. ...

  4. Machine learning (6-Logistic Regression)

    1.Classification However, 2.Hypothesis Representation Python code: import numpy as np def sigmoid(z) ...

  5. 转:汇编中EBP寄存器和ESP寄存器的区别

    EBP和ESP都是汇编中关于指针的寄存器.但是定义不同: (1)ESP:栈指针寄存器(extended stack pointer),其内存放着一个指针,该指针永远指向系统栈最上面一个栈帧的栈顶.(2 ...

  6. palindrome-partitioning-ii leetcode C++

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. pycharm基本使用与破解

    一.pycharm基本使用 pycharm这款ide软件虽然功能强大,但正因为他的强大,所以小白在刚使用这款软件时上手会有点难度,今天我们就来介绍一下ptcharm的基本使用. 1.基本配置 我们安装 ...

  8. 谷粒 | 10 | 阿里云OSS存储对象服务

    阿里云OSS对象存储服务 准备工作 1.在service模块新建子模块service_oss 2.引入pom.xml文件中引入oss服务依赖 <dependencies> <!--a ...

  9. Oracle Error while trying to retrieve text for error ORA-01804

    我在Linux上编译C++程序,有这个错误. 本机情况: Linux上Oracle的安装情况,服务器上有两个Client版本.我在Makefile中使用了高版本的动态库. 原因: 1.首先排查下 tn ...

  10. linux安全 设置登录失败次数后,拒绝登录

    设置登录失败3次后锁定用户300秒可以通过配合文件/etc/pam.d/sshd配置如下 在第一行 #%PAM-1.0 的下一行添加1a auth required pam_tally2.so den ...