LeetCode 3Sum Closest 最近似的3sum(2sum方法)

题意:找到最接近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方法)的更多相关文章
- 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 ...
- 2Sum,3Sum,4Sum,kSum,3Sum Closest系列
1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- 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 ...
- 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 ...
- 3Sum Closest - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...
- 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 ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
随机推荐
- 准备看的ros相关源码
进程通信:lcm 导航:navigation 3D可视化工具:rviz Mobile Robot Programming Toolkit:mrpt 其他: 人体肌肉:simbody openslam ...
- 爬虫库之BeautifulSoup学习(五)
css选择器: 我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list 1)通过 ...
- 理解复杂的const和typedef和指针的关系
// container.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #inclu ...
- 【Linux学习】Linux用户管理2—用户配置文件
Linux用户管理2-用户配置文件 /etc/passwd: 存放系统用户的文件 输入 vi /etc/passwd /etc/shadow: 保存保密文件 /etc/group: 群组文件 输入 v ...
- Maven使用阿里云公共仓库
https://help.aliyun.com/document_detail/102512.html?spm=a2c40.aliyun_maven_repo.0.0.3618305449xZaK
- OPENGL3_基本图元
类型 说明 GL_POINTS 单个顶点集 GL_LINES 多组双顶点线段 GL_POLYGON 单个简单填充凸多边形 GL_TRAINGLES 多组独立填充三角形 GL_QUADS 多组独立填充四 ...
- Educational Codeforces Round 18D(完全二叉树中序遍历&lowbit)
题目链接:http://codeforces.com/contest/792/problem/D 题意:第一行输入n, q,分别表示给出一颗n个节点的中序遍历满二叉树,后面有q个询问; 接下来有q组形 ...
- Mol Cell Proteomics. |阳梦如|富马酸二甲酯在神经元和星形胶质细胞中新蛋白质靶点的鉴定及相关功能验证
大家好,本周分享的是发表在Molecular & Cellular Proteomics.上的一篇关于富马酸二甲酯在脑细胞蛋白质中新作用靶点的鉴定及功能性验证的文章,题目是Identifica ...
- mysql项目实战经验
一.项目的编码设置 目的:避免出现莫名其妙错误,笔者曾经就碰到因编码不对返回null而浪费大量时间:统一的编码可以减少解析的时间,提高效率 1.1修改my.ini文件 一般在C:\Program ...
- JVM图解
站在巨人的肩膀上 https://www.jianshu.com/p/314272e6d35b 1. Minor GC (1) Minor GC过程 假设现在Heap内存大小为20M,其中年轻代为10 ...