3Sum Closest - LeetCode
题目链接
注意点
- 和3Sum那道题的target是0,这道题是题目给定的
- 要先计算误差再移动指针
解法
解法一:做法类似3Sum那道题解法二,每次移动指针前先计算误差,如果误差为0,直接返回target即可。时间复杂度为O(n^2)
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(),nums.end());
int n = nums.size(),mytarget,i = n-1,j,k;
int minError = INT_MAX,ans = target;
while(i >= 2)
{
mytarget = target - nums[i];
j = 0;
k = i-1;
while(j < k)
{
int temp = nums[j]+nums[k];
if(temp < mytarget)
{
if(mytarget - temp < minError)
{
ans = nums[i]+nums[j]+nums[k];
minError = mytarget - temp;
}
j++;
}
else if(temp > mytarget)
{
if(temp - mytarget < minError)
{
ans = nums[i]+nums[j]+nums[k];
minError = temp - mytarget;
}
k--;
}
else
{
return target;
}
}
i--;
while(nums[i+1]==nums[i])
{
i--;
}
}
return ans;
}
};

小结
- 会做3Sum那道题,这题就不难
3Sum Closest - LeetCode的更多相关文章
- 3Sum Closest——LeetCode
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 3Sum Closest leetcode java
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- [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 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- 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 ...
- 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 ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
随机推荐
- 苏州地区--校招IT公司
完整经历了苏州的秋招和春招,在本校和苏州大学跑了许多次的宣讲会,自认为对苏州IT企业的校招有一个充分的认知.原本打算在苏州找一份Java开发的工作,可是发现自己简历连那些公司的简历关都过不去(对双非学 ...
- appium+python+unittest 测试用例的几种加载执行方式
利用python进行测试时,测试用例的加载方式有2种: 一种是通过unittest.main()来启动所需测试的测试模块: 一种是添加到testsuite集合中再加载所有的被测试对象,而testsu ...
- centos7以上安装python3,一条命令搞定。
直接复制下面的命令就搞定 yum install python34 python34-pip python34-setuptools 使用方法: python3 ---.py pip3 install ...
- python处理数据pandas视频资料
python强大数据处理工具pandas视频资料:https://pan.baidu.com/s/17VRd1cgFaKi20drfCgZ8Gg
- 20135316王剑桥 linux第四周课实验笔记
第三章 程序的机器级表示 3.1历史观点 Intel处理器的换代:8086——80286——i386——i486——Pentium——PentiumPro——PentiumII——PentiumIII ...
- MAX值-单元测试
#include<iostream> using namespace std; int Largest(int list[], int length); // list[]:求最大值的函数 ...
- spring冲刺第五天
昨天进行了地图的初步编写,上网查找了错误的原因,改进了源代码,使程序可以执行. 今天继续编写地图代码,完善地图界面,使其变得美观. 遇到的问题:地图的完善比较难.
- C#设置代码只在调试模式下执行
获取一个值,它指示调试器是否已附加到进程. 命名空间:Namespace:System.Diagnostics if (Debugger.IsAttached) { Response.Write(&q ...
- 30_数据库_第30天java_jdbc_(DBUtils)_讲义
今日内容介绍 1.DBUtils 2.连接池 01DButils工具类的介绍个三个核心类 * A: DButils工具类的介绍个三个核心类 * a: 概述 * DBUtils是java编程中的数据库操 ...
- GC 年轻代 老年代 持久代
转载自:http://www.cnblogs.com/yaoyuan23/p/5587548.html 虚拟机中的共划分为三个代:年轻代(Young Generation).老年代(Old Gener ...