题意:找到最接近target的3个元素之和,并返回该和。

思路:用2个指针,时间复杂度O(n^2)。

 int threeSumClosest(vector<int>& nums, int target) {
int sum=nums[]+nums[]+nums[];
sort(nums.begin(), nums.end());
for(int i=; i<nums.size()-; i++)
{
int p=i+, q=nums.size()-;
while( p!=q )
{
int tmp=target-(nums[i]+nums[p]+nums[q]);
if(abs(tmp)<abs(sum-target) ) sum=nums[i]+nums[p]+nums[q];
if(!tmp) return target;
if(tmp>) p++;
else q--;
}
}
return sum;
}

AC代码

LeetCode 3Sum Closest 最近似的3sum(2sum方法)的更多相关文章

  1. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  2. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列

    1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...

  3. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  4. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

  5. LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number

    1.  Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...

  6. 3Sum Closest - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...

  7. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  8. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  9. 【leetcode】3Sum Closest

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

随机推荐

  1. error:未定义的引用

    用qtcreator编程的话,先在.pro文件中这样写: INCLUDEPATH += \ /usr/local/include/ LIBS += \ -L"/usr/local/lib&q ...

  2. npm安装appium server路过的坑

    1.因为appium服务器是用node.js开发的,所以第一步要安装nodejs. 安装后,系统默认配置的环境变量在C盘的用户目录下,为了避免以后下载的包都放在系统盘下, 配置npm下载的包存放目录和 ...

  3. mysql 三存储引擎

    一 什么是存储引擎 mysql中建立的库===>文件夹 库中建立的表===>文件 现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处理文本用txt类型 ...

  4. 609. Find Duplicate File in System

    Given a list of directory info including directory path, and all the files with contents in this dir ...

  5. 基于SpringBoot+MyBatis实现一套电商系统

    项目介绍 mall项目是一套电商系统,包括前台商城系统及后台管理系统,基于SpringBoot+MyBatis实现. 前台商城系统包含首页门户.商品推荐.商品搜索.商品展示.购物车.订单流程.会员中心 ...

  6. c3p0连接池的简单使用和测试1

  7. Django REST framework 的快速入门教程

    CRM-API项目搭建 序列器(Serializers) 首先,我们来定义一些序列器.我们来创建一个新的模块(module)叫做 crm/rest_searializer.py ,这是我们用来描述数据 ...

  8. centOS 部署服务器(三)

    今天一个新的项目终于能够重新安装mysql了,分享下步骤: 1.下载地址:http://dev.mysql.com/downloads/mysql/  (选择Linux - Generic版本的Lin ...

  9. PlSqlDev中执行INSERT SQL语句包含&符号导致数据异常

    在PLSQL Developer中执行Insert语句时提示如下信息: 当时未注意,直接点击OK按钮继续. 导入数据后查看发现部分数据中的参数丢失了一部分,呈以下规律: . 而正常应为: . 经询问大 ...

  10. OS 内存泄漏 导致 整个aix主机block

    问题 aix 主机 1.数据库主机使用vmstat 监控,隔几分钟 就是block 爆满. cpu 没有瓶颈,I/O 显示本地磁盘hdisk0和hdisk 1 是爆满. vmstat 同时显示大量pa ...