一天一道LeetCode系列

(一)题目:

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

For example, given array S = {-1 2 1 -4}, and target = 1. The sum that

is closest to the target is 2. (-1 + 2 + 1 = 2).

(二)解题

直接用三重循环,然后考虑到重复的数字,则需要先排序,以便于后续去重。

其次,当等于target时,则直接返回

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        int min = 2147438647;
        int key =0;
        std::sort(nums.begin() , nums.end());
        for(int i = 0  ; i < nums.size()-2 ; )
        {
            for(int j = i+1 ; j < nums.size()-1 ;)
            {
                for(int k = j+1 ; k < nums.size() ; )
                {
                    int gap = nums[i]+nums[j]+nums[k];
                    int temp = gap-target>0?gap-target:target-gap;
                    if(temp<min){
                        min = temp;
                        key = gap;
                        if(min ==0)  //如果找到等于0的则返回
                        {
                            return key;
                        }
                    }
                    k++;
                    while(k<nums.size() && nums[k] == nums[k-1]) ++k;
                }
                j++;
                while(j<nums.size()-1 && nums[j] == nums[j-1]) ++j;
            }
            i++;
            while(i<nums.size()-2 && nums[i] == nums[i-1]) ++i;
        }
        return key;
    }
};

在网上看到另外一种快速的解法。O(n^2)

class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        std::sort(nums.begin() , nums.end());
        bool isfirst = true;
        int ret;
        for(int i = 0  ; i < nums.size() ; i++)
        {
            int j = i+1;
            int k = nums.size()-1;
            while(j<k){
                int sum = nums[i]+nums[j]+nums[k];
                if(isfirst)
                {
                    ret = sum;
                    isfirst = false;
                }
                else
                {
                    if(abs(sum - target) < abs(ret - target))
                    {
                        ret = sum;
                    }
                }
                if(ret == target)
                    return ret;
                if(sum>target)
                    k--;
                else
                    j++;
            }
        }
        return ret;
    }
};

【一天一道LeetCode】#16. 3Sum Closest的更多相关文章

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

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

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

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

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

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

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

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

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

  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. LeetCode——16. 3Sum Closest

    一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...

  9. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

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

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

随机推荐

  1. 【SSH系列】spring中为什么要使用IOC

    开篇前言 在前面的博文中,小编主要简单的介绍了spring的入门知识,随着学习的深入,我们知道spring最核心的两大技术,IOC和AOP,这两个技术也是spring最耀眼的地方,在后续的博文中小编将 ...

  2. 1小时学会JQuery

    ---------------------------------------------------------------------------------------------------- ...

  3. Android Multimedia框架总结(四)MediaPlayer中从Java层到C++层类关系及prepare及之后其他过程

    转载请把头部出处链接和尾部二维码一起转载,本文出自:http://blog.csdn.net/hejjunlin/article/details/52420803 前言:在上篇中,分析了MediaPl ...

  4. Java继承时的初始化顺序

    Java程序在启动和运行时,需要首先完成初始化的工作.在涉及到继承.static成员变量等因素时,初始化的顺序就复杂起来.下面以一个例子说明继承时的Java初始化顺序. 例子: class Insec ...

  5. Dynamics CRM2015 页面导航栏顶部全局快速查找功能配置

    在CRM2015中微软加入了新的快速查找功能,让你的数据查找更加方便,功能栏如下图所示,直接可以框中输入搜索项进行搜索. 但该功能是需要进行些配置,具体的配置在设置-管理-系统设置中,默认的就是红框中 ...

  6. TCP的发送系列 — tcp_sendmsg()的实现(二)

    主要内容:Socket发送函数在TCP层的实现 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 在上篇blog中分析了tcp_sendmsg()这个主要函 ...

  7. [ExtJS5学习笔记]第五节 使用fontawesome给你的extjs5应用增加字体图标

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/38458411本文作者:sushengmiyan-------------------- ...

  8. UNIX网络编程——通过UNIX域套接字传递描述符和 sendmsg/recvmsg 函数

    在前面我们介绍了UNIX域套接字编程,更重要的一点是UNIX域套接字可以在同一台主机上各进程之间传递文件描述符. 下面先来看两个函数: #include <sys/types.h> #in ...

  9. FORM级别和数据库级别的Trace

     metalink上的文章较全的,中文的可参考我的博客EBS开发技术之trace http://blog.csdn.net/cai_xingyun/article/details/17250971 ...

  10. 操作系统处理内存时内存页为4k

    windows和unix处理内存时,一个内存页的大小都为4k. 测试代码 int main() { while (1) { int *p = (int *)malloc(1024); getchar( ...