https://oj.leetcode.com/problems/3sum-closest/

给一列数和target,在这一列数中找出3个数,使其和最接近target,返回这个target。

一般思路是 n*n*n,优化的话,如下:

先给数排序,然后对第一个数进行遍历 i,第二个数为i+1,第三个数为len-1,看得出的和于target的比较。如果小于target,则第二个数下标++,如果大于target,则第三个数下标--。

class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
int len = num.size();
if(len<)
return ;
if(len == )
return num[]+num[]+num[]; sort(num.begin(),num.end());
int nearAns = num[]+num[]+num[];
for(int i = ;i<len;i++)
{
int j = i+;
if(j>len-)
break;
int k = len-; while(j<k)
{
int sum = num[i]+num[j]+num[k];
if(sum == target)
return target;
if(sum<target)
{
if(abs(target - sum) < abs(target - nearAns))
nearAns = sum;
j++;
}
if(sum>target)
{
if(abs(target - sum) < abs(target - nearAns))
nearAns = sum;
k--;
}
}
}
return nearAns;
}
};

LeetCode OJ-- 3Sum Closest的更多相关文章

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

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

  2. 【leetcode】3Sum Closest

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

  3. [Leetcode][016] 3Sum Closest (Java)

    题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...

  4. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  5. LeetCode (13): 3Sum Closest

    https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...

  6. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  7. Leetcode 16. 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  8. Java [leetcode 16] 3Sum Closest

    题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...

  9. [LeetCode] 16. 3Sum Closest 解题思路

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  10. [LeetCode][Java] 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

随机推荐

  1. Python入门第一课——Python的起源、发展与前景!

    我们在做任何一件事情之前,我们都会通过各种渠道去搜集事情的信息,了解事情的来龙去脉,学习一门编程语言也是如此,只有知根知底,我们才能有明确的方向和目标,以及底气去完成这件事情,今天我带大家来看看Pyt ...

  2. UVA_1025 a Spy in the Metro 有向无环图的动态规划问题

    应当认为,有向无环图上的动态规划问题是动态规划的基本模型之一,对于某个模型,如果可以转换为某一有向无环图的最长.最短路径问题,则可以套用动态规划若干方法解决. 原题参见刘汝佳紫薯267页. 在这个题目 ...

  3. Maya

    建立酒杯的方法(CV曲线) surface(曲面)-- creat cv curve tool-- control vertex(调整图形)[再次creat cv建立厚度,只需要建立酒杯的上口]--- ...

  4. linux学习(二) -- ubuntu下lnmp环境的配置

    亲测的教程,,希望能对大家提供些许帮助,转载请注明出处 ubuntu+nginx+mysql+php7 一.安装Nginx 1.首先添加nginx_signing.key(必须,否则出错) $ wge ...

  5. rn-fetch-blob+redux 取消请求

    其实取消请求对于普通的ajax请求rn-fetch-blob写法是比较简单的 let task = RNFetchBlob.fetch('GET', 'http://example.com/file/ ...

  6. STL之string使用简介

    声明一个C++字符串 string类的构造函数和析构函数如下: string s; //生成一个空字符串s string s(str) //拷贝构造函数 生成str的复制品 string s(str, ...

  7. 【Luogu】P2403所驼门王的宝藏(强连通分量)

    题目链接 想到缩点后DP这题就迷之好做 横天门就点向该行连一条边 纵门就点向该列连一条边 ziyou门直接枚举……map搞搞……话说ziyou门为啥是违规内容不让我发布? 然后缩点,DP,1A 不过写 ...

  8. hihoCoder [Offer收割]编程练习赛83 D 生成树问题

    题目 从 Kruskal 算法的角度来思考这个问题. 考虑 $n$ 个点的"空图"(即没有边的图). 先将 $m_2$ 条无权值的边加到图中,得到一个森林. 按边权从小到大的顺序枚 ...

  9. SYZOJ 186 [额]你猜是不是DP(哈希+二分答案+二分搜索)

      题目描述 现在给两个仅包含小写字母的字符串a,b ,求a 与b的最长公共连续子串的长度. 输入格式 两个字符串 输出格式 一个整数,为输入的两个字符串的最长公共连续子串的长度 测试样例 输入 qa ...

  10. uvalive 6323 状态压缩DP

    思路:dp[i][j][x]表示状态 i 以 j 为结束 得分为 x 的方案数. #include<iostream> #include<cstdio> #include< ...