题意:找到最接近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. 1.10-1.11 hive交互式命令讲解

    一.hive 交互式命令参数 #帮助 [root@hadoop-senior hive-0.13.1]# bin/hive -h Missing argument for option: h usag ...

  2. linux中用管道实现兄弟进程通信

    1 使用fork函数创建两个子进程.在第一个子进程中发送消息到第二个子进程,第二个子进程都出来并处理. 2 在父进程中,不适用管道通信,所以什么不需要做直接关闭勒管道的两端 3 代码实现 #inclu ...

  3. jquery登录的异步验证

    //定义一个json var validate = { username : false, pwd : false, pwded : false, verify : false, loginUsern ...

  4. python 三元表达式 列表推导式,生成器表达式。递归,匿名函数, 内置函数

    三元表达式 三元表达式仅应用于: 1.条件成立返回一个值 2.条件不成立返回一个值 res = x if x>y else y print(res) name= input("姓名&g ...

  5. CodeForces - 357C Knight Tournament 伪并查集(区间合并)

    Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...

  6. HTML学习笔记(六)TCP/IP

    TCP/IP 是供已连接因特网的计算机进行通信的通信协议. 在 TCP/IP 中包含一系列用于处理数据通信的协议: TCP (传输控制协议) - 应用程序之间通信 UDP (用户数据包协议) - 应用 ...

  7. Axure RP 7.0 标准教程(1)

    一. Axure RP 标准教程 1. 为什么学习 增加沟通效率

  8. 线程通讯-Condition

    Account类 package com.thread.communication.condition; import java.util.concurrent.TimeUnit; import ja ...

  9. Java的多线程创建方法

    1. 直接使用Thread来创建 package com.test.tt; public class ThreadEx extends Thread{ private int j; public vo ...

  10. C++泛型编程之函数模板

    泛型语义 泛型(Generic Programming),即是指具有在多种数据类型上皆可操作的含意.泛型编程的代表作品 STL 是一种高效.泛型.可交互操作的软件组件. 泛型编程最初诞生于 C++中, ...