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. 03-oracle数值函数

    --round(数值,想要保留的位数如1.2.3),作用:保留小数取值时四舍五入select round(678.656) 不保留小数,round(678.456,1) 保留一位小数,round(67 ...

  2. openerp学习笔记 模块结构分析

    以OpenERP7.0中的 hr_expense 模块为例: 如图中代码所示: __init__.py :和普通 Python 模块中的__init__.py 作用相同,主要用于引用模块根目录下的.p ...

  3. CoreJava笔记之线程

    程序,进程和线程程序:没有执行的指令序列和相关的数据的集合(如:qq.exe) 如:磁盘上的可执行命令进程:正在执行的程序,进程占用资源(CPU,Memoary,IO)线程:是进程中并发执行的过程(共 ...

  4. Node.js中http-server的使用

    Node.js中http-server的使用 使用阿里的npm镜像 国外的npm太慢了.查看一下自己使用的源: npm config get registry 应该显示https://registry ...

  5. Mac新手问题

    [现象:]MAC下checkout代码时,报svn:E170013-unable to connect to a repository at url xxx, svn:E1700113: unable ...

  6. SSH基本概念和用途

    一.SSH是什么 简单的说,SSH是一种网络协议,主要用于客户端与远程主机的安全链接和交互. 二.常见用法1.客户端与远程主机的安全链接命令如下: $ ssh -p user@host 解释如下:-p ...

  7. Scrapy框架学习(三)Spider、Downloader Middleware、Spider Middleware、Item Pipeline的用法

    Spider有以下属性: Spider属性 name 爬虫名称,定义Spider名字的字符串,必须是唯一的.常见的命名方法是以爬取网站的域名来命名,比如爬取baidu.com,那就将Spider的名字 ...

  8. 【关于使用SpringJUnit4ClassRunner单元测试报错问题】

    今天单元测试如下的代码的时候发现项目老是报错: package com.yhb.jsxn.service; import java.text.SimpleDateFormat; import java ...

  9. Java 实例 - 标签(Label)

     Java 实例 Java 中的标签是为循环设计的,是为了在多重循环中方便的使用break 和coutinue . 以下实例当在循环中使用 break 或 continue 循环时跳到指定的标签处: ...

  10. rabbit工作队列模式

    工作队列比简单队列在消费者这边多了一个方法. channel.basicQos(1);公平队列消费(参数设置为1,表示消费者消费完一条才会去接受再次发来的消息) 生产者: package com.kf ...