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.

  1. For example, given array S = {-1 2 1 -4}, and target = 1.
  2. 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函数,求绝对值。

代码:

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

  1. class Solution {
  2. public:
  3. int threeSumClosest(vector<int> &num, int target) {
  4. sort(num.begin(), num.end());
  5. int size = num.size();
  6. int min_gap = INT_MAX;
  7.  
  8. for (int i = ; i < size; ++i) {
  9. int j = i + ;
  10. int k = size - ;
  11.  
  12. while (j < k) {
  13. int cur_gap = num[i] + num[j] + num[k] - target;
  14. if (abs(cur_gap) < abs(min_gap))
  15. min_gap = cur_gap;
  16.  
  17. if (cur_gap > )
  18. --k;
  19. else if (cur_gap < )
  20. ++j;
  21. else
  22. return target;
  23. }
  24. }
  25.  
  26. return target + min_gap;
  27. }
  28. };
  1.  

【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. XML 十六进制值 是无效的字符错误 解决方法之一 转

    /// <summary> /// 过滤非打印字符 /// </summary> /// <param name="tmp">待过滤</p ...

  2. [JAVA]流控及超流控后的延迟处理

    流控检查(每半秒累计,因此最小留空阀值只能做到每秒2条): import java.text.SimpleDateFormat; import java.util.Date; import java. ...

  3. 【Css】一个简单的图片库

    今天做一个简单的图片库! 其实这个在w3school教程里介绍得很好了,不过看到什么,自己动手做一次,记得也深刻不是. 我们分几步来走: 第一步:先写一个坯子. <html> <he ...

  4. C# 获取SHA256码

    1. 如果是要获得某个字符串的SHA256,代码如下: public static string SHA256(string str) { //如果str有中文,不同Encoding的sha是不同的! ...

  5. Maven 常见知识点整理

    认识 Maven Maven 的作用? 1.添加第三方jar包 2.解决jar包之间的依赖关系 3.获取第三方jar包 4.将项目拆成多个工程模块 Maven 是什么? 是Apache软件基金会组织维 ...

  6. 朝圣Java(问题集锦)之:The Apache Tomcat installation at this directory is version 8.5.32. A Tomcat 8.0 inst

    最近开始学Java了.有C#底子,但是学起来Java还是很吃力,感觉别人架好了各种包,自己只要调用就行了,结果还有各种bug出现.掩面中. 启动Tomcat的时候,报错The Apache Tomca ...

  7. 很多个java面试题

    1. 为什么说Java是一门平台无关语言? 平台无关实际的含义是“一次编写到处运行”.Java 能够做到是因为它的字节码(byte code)可以运行在任何操作系统上,与底层系统无关. 2. 为什么 ...

  8. MyBaits_查询缓存02_Ehcache二级缓存

    一.Ehcache二级缓存的开启 导入jar(https://github.com/mybatis/ehcache-cache/releases) <cache type="org.m ...

  9. python对excel文件的读写操作

    import xlrd,xlwt data = xlrd.open_workbook('a.xlsx') #读 table = data.sheets()[0] data_list = [] data ...

  10. FLASK日志记录

    from flask import Flask from flask_restful import Resource, Api import logging app = Flask(__name__) ...