题目链接

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. Scikit-learn数据变换

    转载自:https://blog.csdn.net/Dream_angel_Z/article/details/49406573 本文主要是对照scikit-learn的preprocessing章节 ...

  2. Python-RabbitMQ(简单发送模型)

    RabbitMQ需要 erlang 和pika 1.RabbitMQ和erlang版本必须匹配,否则就报没有进程错误 2.RabbitMQ的erlang.cookie和windows下的erlang. ...

  3. ofo容器pass架构分享

    一.我们先要了解一下,为什么企业需要一个paas平台?或者可以说paas到底能做什么? 1.1 我们先来了解一下paas到底是什么? PaaS是Platform-as-a-Service的缩写,意思是 ...

  4. Docker 私有仓库方案比较与搭建

    我们知道docker镜像可以托管到dockerhub中,跟代码库托管到github是一个道理.但如果我们不想把docker镜像公开放到dockerhub中,只想在部门或团队内部共享docker镜像,能 ...

  5. XSS跨站脚本

    1.反射型 非持久化,需要用户自己点击才可以触发 通常出现在搜索框 <?php $id=$_GET['id']; echo $id; ?> http://127.0.0.1/test/sc ...

  6. django之基本配置

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  7. iOS中使用RNCryptor对资源文件加密(先加密后拖进项目中)

    概述:IPA 在发布时,业务相关的敏感资源文件以明文的形式存储,由于没有加密保护,这些文件在应用发布后 可能被其他人获取,并结合其他漏洞和手段产生真实攻击.所以我们要 1.在设计.开发阶段,集合业务确 ...

  8. 第二阶段Sprint冲刺会议1

    进展:总结第一阶段冲刺成就,讨论第二阶段任务,要实现的主要功能,分工及任务认领.

  9. 【CS231N】5、神经网络静态部分:数据预处理等

    一.疑问 二.知识点 1. 白化 ​ 白化操作的输入是特征基准上的数据,然后对每个维度除以其特征值来对数值范围进行归一化.该变换的几何解释是:如果数据服从多变量的高斯分布,那么经过白化后,数据的分布将 ...

  10. DPDK skeleton basicfwd 源码阅读

    学习这个例子用于理解单纯的 dpdk 转发过程,L2 和 L3 的转发是基于此:在rte_eth_rx_burst()收包后进行解包,提取 mac.ip 等信息然后在转发到输出网卡. 如果要写出自己的 ...