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-standard library-9.6 random

    笔记-python-standard library-9.6 random 1.      random source code:Lib/random.py 1.1.    functions for ...

  2. 如何在C#中调试LINQ查询

    原文:How to Debug LINQ queries in C# 作者:Michael Shpilt 译文:如何在C#中调试LINQ查询 译者:Lamond Lu 在C#中我最喜欢的特性就是LIN ...

  3. c++实验4

    1. 车辆基本信息管理  #include <iostream> using namespace std; #include <string> #include "c ...

  4. JAVA后端常用框架SSM,redis,dubbo等

    JAVA后端常用框架SSM,redis,dubbo等   一.SpringMVC http://blog.csdn.net/evankaka/article/details/45501811 spri ...

  5. 二叉树遍历(Java实现)

    二叉树遍历(Java实现)   主要是二叉树的遍历,包括递归遍历和非递归遍历 import java.util.ArrayDeque; import java.util.ArrayList; impo ...

  6. leetcode 【 Search in Rotated Sorted Array II 】python 实现

    题目: 与上一道题几乎相同:不同之处在于array中允许有重复元素:但题目要求也简单了,只要返回true or false http://www.cnblogs.com/xbf9xbf/p/42545 ...

  7. C++ STL map容器的说明测试1

    // maptest.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h" /*********************************** ...

  8. phpmyadmin4.8.1后台getshell

    phpmyadmin4.8.1后台getshell 包含文件进行getshell 姿势: ① 建立数据库的,新建表,字段名为一句话木马. 会生成对应的数据库文件,相应文件的路径查看 select @@ ...

  9. APK无源码使用Robotium简单总结

    1.使用re-sign.jar对待测包进行重签名,并记录下包名和主Activity名. 2.在Eclipse中点击File-New-Other 选择Android下的Android Test Proj ...

  10. Selenium - WebDriver Advanced Usage

    Explicit Waits # Python from selenium import webdriver from selenium.webdriver.common.by import By f ...