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

解题:

数字求和问题,除了使用hash表使查找复杂度降为o(1)外,没有其他特别的方法。基本的思路都是枚举某一个数,然后计算余下的数字组合;

本题先对数组进行排序,然后以某一个数为基准,设置两个指针从两头操作余下的数,计算三数的和,如果和大于target,右指针左移,反之,左指针右移。期间不断记录离target最近的sum值。

总时间复杂度o(nlogn) + o(n2) = o(n2)

需要用到C++ abs函数,求绝对值。

代码:

(由于题目中说一定存在一个答案,因此省略判断某些边界情况)

 class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
sort(num.begin(), num.end());
int size = num.size();
int min_gap = INT_MAX; for (int i = ; i < size; ++i) {
int j = i + ;
int k = size - ; while (j < k) {
int cur_gap = num[i] + num[j] + num[k] - target;
if (abs(cur_gap) < abs(min_gap))
min_gap = cur_gap; if (cur_gap > )
--k;
else if (cur_gap < )
++j;
else
return target;
}
} return target + min_gap;
}
};
												

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

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【leetcode刷题笔记】3Sum Closest

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

  6. 【LeetCode每天一题】3Sum Closest(最接近的三数和)

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  7. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

  8. 【leetcode刷题笔记】3Sum

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

  9. 【LeetCode从零单排】No15 3Sum

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

  10. 【LeetCode每天一题】3Sum(三数之和)

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

随机推荐

  1. 解决C#中dynamic类型作为泛型参数的反射问题

    C#中dynamic类型作为泛型参数传递过去后,反射出来的对象类型是object,我用老外的这篇博文中的代码跑起来,得出的结果是:Flying using a Object map (a map),将 ...

  2. 使用discover批量执行用例

    TestLaoder 该类负责根据各种条件加载测试用例,并将它们返回给测试套件,正常情况下,不需要创建这个类的实例,unittest提供了可以共享的defaultTestLoader类,可以使用其子类 ...

  3. 关于echart x轴溢出的解决办法

    现象 溢出挤在一起,或者默认多余的不显示,如果需要强制显示,需要设置 axisLabel: { interval: 0, } 解决办法: 一.旋转一定的角度 axisLabel: { interval ...

  4. Object-c 中的数据类型

    导航:  基本类型  ID 对象类型常见的有 对象类型 -NSLog -NSNumber -NSString和NSMutableString -NSArray和NSMutableArray -NSSe ...

  5. Linux上用户之间对话

    Linux上用户之间对话 昨天想在CentOS7上与另外一个用户对话,但把命令忘记了,特此记录下来. Write命令 write命令是单向发送一条消息给同机器的Linux用户.首先通过who命令查看谁 ...

  6. UEditor编辑器 字符数统计和字符数限制 问题

    1.百度UEditor修改右下角统计字数默认只统计前台所见的文字个数,为了便于展示实际保存的时候是保存的包含html标签的,所以右下角的统计字数功能需要修改 getContentLength: fun ...

  7. Java中break、continue及标签等跳转语句的使用[下]

    作为上一篇使用for循环演示的跳转,这一篇将使用while.相比较来说,while比for循环更简单.代码如下: public class LabeledWhile { public static v ...

  8. luajit+nginx+上传模块+lua模块编译安装

    git clone https://github.com/fdintino/nginx-upload-module.git git clone https://github.com/openresty ...

  9. jquery获取子元素

    Jquery获取子元素的方法有2种,分别是children()方法和find()方法. 下面我们分别来使用这两种方法,看看它们有何差异. children()方法:获取该元素下的直接子集元素 find ...

  10. .Net Core GB2312编码问题

    1.今天抓取了一个网页的源代码.发现中文是乱码的,马上第一反应是编码问题..... 2.仔细一看基于WebClient写的代码,还真的是没有设置编码... /// <summary> // ...