【leetcode】3Sum Closest

时间:2022-11-12 22:05:00

3Sum Closest

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).
 
与 3Sum类似,只是需要添加一个变量distance,用来记录和target距离最小的a+b+c
 
 class Solution {
public:
int threeSumClosest(vector<int> &num, int target) { int n=num.size();
sort(num.begin(),num.end()); int i,j,k;
int a,b,c;
int result;
int distance=INT_MAX; for(i=;i<n-;i++)
{
j=i+;
k=n-; while(j<k)
{
a=num[i];
if(i>&&num[i]==num[i-])
{
break;
}
b=num[j];
if(j>i+&&num[j]==num[j-])
{
j++;
continue;
} c=num[k];
if(k<n-&&num[k]==num[k+])
{
k--;
continue;
} if(a+b+c==target)
{
return target;
}
else if(a+b+c<target)
{
if(target-(a+b+c)<distance)
{
result=a+b+c;
distance=target-result;
}
j++;
}
else if(a+b+c>target)
{
if((a+b+c)-target<distance)
{
result=a+b+c;
distance=result-target;
}
k--;
}
}
} return result; }
};

【leetcode】3Sum Closest的更多相关文章

  1. 【LeetCode】3Sum Closest 解题报告

    [题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...

  2. 【leetcode】3Sum Closest(middle)

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  3. 【LeetCode】3Sum 解决报告

    这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...

  4. 【Leetcode】【Medium】3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  5. 【leetcode】3Sum

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  6. 【leetcode】3Sum &lpar;medium&rpar;

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  7. 【LeetCode】270&period; Closest Binary Search Tree Value 解题报告&lpar;C&plus;&plus;&rpar;

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  8. 【LeetCode】双指针 two&lowbar;pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  9. 【LeetCode】849&period; Maximize Distance to Closest Person 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Euclid求最大公约数

    Euclid求最大公约数算法 #include <stdio.h> int gcd(int x,int y){ while(x!=y){ if(x>y) x=x-y; else y= ...

  2. http 301 和 302 的区别!

    1.什么是301转向?什么是301重定向? 301转向(或叫301重定向,301跳转)是当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息(header)中的状态码的一种, ...

  3. LintCode 463 Sort Integer

    这个是O(n2)的排序的总结 /* bubble sort */public static void sortIntegers(int[] A) { // Write your code here i ...

  4. 删除MSSQL数据库中所有表

    --删除所有约束 DECLARE c1 cursor for select 'alter table ['+ object_name(parent_obj) + '] drop constraint ...

  5. VMware11安装Mac OS X10提示不可恢复错误解决

    VMware11安装Mac OS X10提示不可恢复错误(vcpu-0)怎么办?本文将详细介绍如何解决VMware11安装Mac OS X10提示不可恢复错误. 工具/原料   VMware11 PC ...

  6. &lbrack;016&rsqb;转--C&plus;&plus;拷贝构造函数详解

    一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: int a = 100; int b = a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员 ...

  7. python mmap对象

    ----使用内存映射的原因 为了随机访问文件的内容,使用mmap将文件映射到内存中是一个高效和优雅的方法.例如,无需打开一个文件并执行大量的seek(),read(),write()调用,只需要简单的 ...

  8. 关于COM类工厂80070005和8000401a错误分析及解决办法

    关于COM类工厂80070005和8000401a错误分析及解决办法 看到很多相关的文章,第一次配置配置时没有啥作用,让别人来解决的,可惜不晓得他怎么解决的,当我再次遇到时,不得不硬着头皮去解决. 总 ...

  9. stm-ledstrip &colon; Driver and test routine for WS2811 RGB-LED

    stm-ledstrip : Driver and test routine for WS2811 RGB-LED #include "ws2812.h" #include &lt ...

  10. C&num; Winform Label内容根据其宽度自动调整字体大小

    C# Winform Label内容根据其宽度自动调整字体大小 项目,有个要求,Label中显示的内容,能够根据其宽度自动调整字体的大小进行显示,刚刚开始的时候,总是想着通过不同的方法来直接测量内容的 ...