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 (medium)

    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. Closest Binary Search Tree Value 解题报告(C++)

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

  8. 【LeetCode】双指针 two_pointers(共47题)

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

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

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

随机推荐

  1. javaee添加验证码

    package com.zh.code; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import j ...

  2. iOS开发中的错误整理,(百思项目,指示器位置)设置控件尺寸和点坐标,先设置尺寸,再设置点坐标

    之前对控件的尺寸和点的坐标的设置从来都是想到什么写什么,从来没有关心过顺序.然后就有了这次的血的教训!!!!! 下面是错误的截图,先设置的中心点,然后设置的宽度.程序运行就这样了,点别的没有毛病!!! ...

  3. Hibernate-缓存-并发策略

    当多个并发的事务同时访问持久化层的缓存中的相同数据时,会引起并发问题,必须采用必要的事务隔离措施. 在进程范围或集群范围的缓存,即第二级缓存,会出现并发问题.因此可以设定以下4种类型的并发访问策略,每 ...

  4. iframe标签用法详解(属性、透明、自适应高度)

    1.iframe 定义和用法 iframe 元素会创建包含另外一个文档的内联框架(即行内框架). HTML 与 XHTML 之间的差异 在 HTML 4.1 Strict DTD 和 XHTML 1. ...

  5. codeforces 375D:Tree and Queries

    Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  6. IQ测试

    1.4个人过桥,只能两两过桥,且只有一盏灯,必须有灯才能过桥,4个人过桥时间分别为1,2,5,10分钟,最短多少时间可以过桥? 答案:1和2先走,1再返回,花3分钟.5和10走,2回去,花了3+10+ ...

  7. 了解 Nginx 基本概念

    前言 本篇是我学习 Nginx 的一些笔记,主要内容讲述了一些了解 Nginx 需要的基本概念.然后探讨一下 Nginx 的模块化的组织架构,以及各个模块的分类.工作方式.职责和提供的相关指令. 主要 ...

  8. 如何通俗地理解 Gradle

    http://www.zhihu.com/question/30432152 一句话概括就是:依赖管理和任务执行. 像Ruby里面的bundler+rake,像iOS中的cocoapods,像node ...

  9. MongoDB的增删改查 转

    MongoDB的增删改查 (黎明你好原创作品,转载请注明) MongoDB中数据的基本单元叫做文档,采用json的键-值的方式.多个键及其关联的值有序的存放在一起变是文档.类似于编程语言中的键值关系. ...

  10. hdu 1205 吃糖果

    思路: 仔细想想,想要不重复吃一种糖果, 把所有糖果吃完,只要所有糖果的和,减去最多的糖果+1>=最多糖果的数量即可不重复吃完. #include <stdio.h> int mai ...