class Solution {
public:
void quick_order(vector<int>& num, int star, int en)//快排
{
int start = star;
int end = en;
if (start >= end)
return; int index = num[start];//就第一个设为阈值
while (start < end)
{
while (start < end && index <= num[end])//先看右边,把第一个小于index数找出
end--; int temp1 = num[start];//交换
num[start] = num[end];
num[end] = temp1; while (start < end && index >= num[start])//再看右边,把第一个大于index的数找出
start++; int temp2 = num[start];//交换
num[start] = num[end];
num[end] = temp2;
} quick_order(num, star, start-);//分左右子集迭代
quick_order(num, start + , en);
return;
} int threeSumClosest(vector<int>& nums, int target) {
int len=nums.size();
long res=*INT_MAX; if(len==)//输入判断
return ; int start=,end=len-;
quick_order(nums,start,end);//快排 for (int c = nums.size()-; c >= ; --c)
{
int tar=target-nums[c];//设置a和b要找的数字
for (int a = , b = c-; a < b; )
{
if (abs(res)>=abs(nums[a]+nums[b]+nums[c]-target))//先检验是否更接近target
res=nums[a]+nums[b]+nums[c]-target; int tmp_sum = nums[a]+nums[b];//再进行下一步迭代
if (tmp_sum < tar)
++a;
else
--b;
}
}
return int(res+target); }
};

分析:

和上个类似,也是O(n^2)时间复杂度遍历。

leecode第十六题(最接近的三数之和)的更多相关文章

  1. Leetcode题库——16.最接近的三数之和

    @author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...

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

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

  3. lintcode-59-最接近的三数之和

    59-最接近的三数之和 给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和. 注意事项 只需要返回三元组之和,无需返回三元组本身 样例 例如 S = ...

  4. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  5. Java实现 LeetCode 16 最接近的三数之和

    16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...

  6. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

  7. LeetCode-016-最接近的三数之和

    最接近的三数之和 题目描述:给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只 ...

  8. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  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. 最接近的三数之和(给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数, 使得它们的和与 target 最接近。返回这三个数的和)

    例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 思路:首先对数组进行排序     ...

随机推荐

  1. hihocoder [Offer收割]编程练习赛8

    第一次做这种比赛,被自己坑的好惨... A.这道题的关键其实是如果有k和n满足kD+F>nL>kD则不能走无限远,分支看似难整理,其实比较简单,F>L根本就不用算了,明摆着就是Bsi ...

  2. django模板-通过a标签生成链接并跳转

    views.py from django.shortcuts import render from django.http import HttpResponse def index(request) ...

  3. 安装python3.7和PyCharm专业版

    安装python3.7 安装PyCharm专业版 1.下载地址http://www.jetbrains.com/pycharm/download/,选择windows下面的Professional,点 ...

  4. 关于PRD、MRD、BRD文档

    笔者现在所在的公司有专职的产品经理,在职业生涯最长的那家公司,并没有专门的产品经理,虽然在创业公司期间有产品经理,但是似乎产品经理更多的是和客户沟通需求,反馈,并不输出正规意义上产品经理该有的输出.从 ...

  5. Zookeeper注册中心的搭建

    一.Zookeeper的介绍 Zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用 ...

  6. Redis热点Key发现及常见解决方案!

    一.热点Key问题产生的原因 1.用户消费的数据远大于生产的数据(热卖商品.热点新闻.热点评论.明星直播). 在日常工作生活中一些突发的的事件,例如:双十一期间某些热门商品的降价促销,当这其中的某一件 ...

  7. 01:saltstack 基本使用

    1.1 准备实验环境: 安装系统 1)硬件配置如下 2) 先把光标放到”install CentOS 7”,按 Tab键编辑内核参数,添加 (net.ifnames=0 biosdevname=0) ...

  8. Sublime Text安装与配置

    1.1 下载安装Sublime Text 3 参考博客:https://www.cnblogs.com/Rising/p/3741116.html 1.下载安装Sublime Text 3  1. 下 ...

  9. 记录结果再利用的"动态规划"之背包问题

    参考<挑战程序设计竞赛>p51 https://www.cnblogs.com/Ymir-TaoMee/p/9419377.html 01背包问题 问题描述:有n个重量和价值分别为wi.v ...

  10. noip模拟【noname】

    noname [问题描述] 给定一个长度为n的正整数序列,你的任务就是求出至少需要修改序列中的多少个数才能使得该数列成为一个严格(即不允许相等)单调递增的正整数序列,对序列中的任意一个数,你都可以将其 ...