leecode第十六题(最接近的三数之和)

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第十六题(最接近的三数之和)的更多相关文章
- Leetcode题库——16.最接近的三数之和
@author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- lintcode-59-最接近的三数之和
59-最接近的三数之和 给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和. 注意事项 只需要返回三元组之和,无需返回三元组本身 样例 例如 S = ...
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合
> 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 最接近的三数之和(给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数, 使得它们的和与 target 最接近。返回这三个数的和)
例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 思路:首先对数组进行排序 ...
随机推荐
- K8S学习笔记之二进制的方式创建一个Kubernetes集群
0x00 单节点搭建和简述 minikube Minikube是一个工具,可以在本地快速运行一个单点的Kubernetes,尝试Kubernetes或日常开发的用户使用.不能用于生产环境. 官方地址: ...
- 禁止单个IP或ip段访问
//IP禁止判断接口,返回true则为找到 function checkIp($ip, $ipbanned) { $ipbannedFlag = false; if (!empty($ipbanned ...
- 过滤特殊字符(包括过滤emoji表情)
/** * 过滤特殊字符 * @param $text * @return mixed */ public static function filterSpecialChars($text) { // ...
- oracle 18c的版本号规则
18C之后的版本标识 从2017年7月开始,Oracle改变了以往的数据库软件发布流程,采用年度Release和季度更新的策略. Yearly Release 将之前的N年一发布更改为每年一发布.每年 ...
- Hacker
https://hackertarget.com/nikto-website-scanner/
- Python3 tkinter基础 Canvas create_polygon 画三角形
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- grub基本应用
一.基本概念 GRUB(boot loader): GRand Unified Bootloader 两个版本: grub .x: grup legacy grub .x: grub2 grub ...
- Ubuntu 18.04 修改gedit的配色方案
下图中的蓝色的注释代码,真是有点让人瞎眼的感觉 去这个网站 https://github.com/mig/gedit-themes/tree/master 下载所有后解压到/usr/share/gtk ...
- 用yarn代替cnpm,cnpm漏包有点严重
npm 的方式 npm install -g yarn 安装完成后,你可以测试下自己的版本 yarn --version 开始使用 单独安装包的方式add 不是install,后面不用加 ...
- 微服务架构与实践3_api
场景分析 描述产品服务,基于REST的接口 表述性状态转移(Representational State Transfer,REST) 任务拆分 将整体要做的工作内容划分成小的任务: 统一时间聚焦一个 ...