一天一道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. PHP Ajax JavaScript 实现 无刷新附件上传

    普通表单 前端页面 后台处理 带有文件的表单 刷新方式 前端界面 后台页面 无刷新方式 大文件上传 POST极值 upload极值 上传细节 前端页面 后台处理 总结 对一个网站而言,有一个基本的不可 ...

  2. Eric5 for Python 3.3.3安装指南

    一言蔽之,搭配是关键.以32位Window为例,先后安装: 1.PyQt PyQt4-4.10.3-gpl-Py3.3-Qt4.8.5-x32.exe http://www.riverbankcomp ...

  3. springMVC源码分析--动态样式ThemeResolver(一)

    Spring MVC中通过ThemeSource接口来提供对动态更换样式的支持,并提供了ResourceBundleThemeSource这个具体实现类来提供通过properties配置文件对them ...

  4. Java基本语法-----java数组(一维数组二维数组)

    嘿嘿!你们懂的,又是图片,委屈大家了. java数组(一维数组二维数组) [正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!下面有个"顶"字,你就 ...

  5. 安卓高仿QQ头像截取升级版

    观看此篇文章前,请先阅读上篇文章:高仿QQ头像截取: 本篇之所以为升级版,是在截取头像界面添加了与qq类似的阴影层(裁剪区域以外的部分),且看效果图:   为了适应大家不同需求,这次打了两个包,及上图 ...

  6. linux中查看现在使用的shell是ksh还是bash?以及怎样修改?

    查看系统支持的shell: cat  /etc/shells 查看现在使用的shell:  修改默认shell: 另外,修改了系统默认shell之后不会立即生效,之后再次登录系统修改的shell才会生 ...

  7. shell 数据流重定向操作符总结

    最近看了鸟哥私房菜关于shell数据流重定向的内容,总结一下. 操作符: 1.标准输入(stdin):代码为0,符号:< 或者<< 2.标准输出(stdout):代码为1,符号:&g ...

  8. Xcode中不用Storyboard,用纯xib创建TabBar模式视图

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如果要开发Tab类型视图的App,在Xcode中可以使用对应的 ...

  9. JQuery实战---窗口效果

    在前面的相关博文中,小编对jquery的相关知识进行了简单的总结,关于jquery的很多小的知识点,都需要我们自己去动手和实践,一行行代码都需要我们自己亲自动手去敲,今天我们继续来学习jquery的相 ...

  10. Java异步通信

    服务器端: import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; impo ...