一天一道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. KVO and Swift

    不像Objective-c中的类,Swift类对于KVO并没有原生的支持,不过你可以在类型安全的前提下使用属性观察者轻松的完成相同的目标. 不管如何,从NSObject类派生出的类是支持KVO的,如果 ...

  2. Android底层开发经验

    最近看到一个博客,他的博文虽然是转载的,但源作者肯定对底层的理解可谓是非常透彻,一副思维导图就可以将整个重要体系建立起来,非常适合大家学习.学习不单单只要有代码,生动有趣更重要.在此推荐一波: htt ...

  3. 2017年校园招聘ios面试题

    一.搜狐快站 1.谈谈你做过的项目: 2.项目中最有成就感的部分: 3.倒计时如何实现?(NSTimer,还有其他的实现方式吗): 4.UIButton的继承关系? 5.iOS中可以进行输入的控件?( ...

  4. Spring3+Hibernate4连接Oracle11g数据库参数配置

    应用场合:使用SSH框架开发一套应用系统,因为不同的SSH版本+系统架构会导致各种的错误,总结测试了下,成功测试得出本文配置 软件版本:Sping3+Hibernate4+Maven3 主要配置文件内 ...

  5. list标准函数的模拟

    ;反序 ( ) -> ( ) (define (rvs x) (let recur ((x x)(res '())) (if (null? x) res (recur (cdr x) (cons ...

  6. iOS网络基础

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51376048 本文出自:[openXu的博客] 常用类 get请求 post请求 NSURL ...

  7. TCP的发送系列 — 发送缓存的管理(一)

    主要内容:TCP发送缓存的初始化.动态调整.申请和释放. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 数据结构 TCP对发送缓存的管理是在两个层面上进 ...

  8. [ExtJS5学习笔记]第十二节 Extjs5开发遇到的问题列表记录

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

  9. 人类创造未来的思想先锋:这些 TED 演示深深震撼着我们

    今年亮点之一是谷歌创始人拉里佩奇的演讲.他有一个核心观点:特别成功的公司,是那些敢于想象未来,并付出行动创造未来的公司.这听上去是老生常谈,但又确实是个真理.他实际上想说预测未来的最好方式就是创造它, ...

  10. 查全率(召回率)、精度(准确率)和F值

    文献中的recall rate(查全率或召回率) and precision(精度)是很重要的概念.可惜很多中文网站讲的我都稀里糊涂,只好用google查了个英文的,草翻如下:召回率和精度定义: 从一 ...