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). 思路:首先对数组进行排序 ...
随机推荐
- Vue小案例 之 商品管理------删除商品与提示
实现删除商品功能 根据索引来进行删除商品: 实现删除商品的HTML: <!--显示表格--> <div class="table-warp"> <di ...
- 深度挖掘,Html5的 Range 滑动刻度的坑,兼容全平台,将任性进行到底!
最近2天一直在弄一个滑动的刻度效果,由于项目是基于Web App开发的,于是考虑到 移动端和pc端 的兼容性问题,考虑的比较多,尝试采用 Html5的Range 来做,目前已经兼容 pc端和移动端! ...
- Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...
- ODAC(V9.5.15) 学习笔记(四)TCustomDADataSet(4)
6.Options TCustomDADataSet的选择项为TDADataSetOptions,其成员介绍如下表 : 名称 类型 说明 TDADataSetOptions AutoPrepare B ...
- Python3 tkinter基础 Frame bind 敲击键盘事件 将按键打印到console中
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 关于link标签的用法, 不声明rel=stylesheet则无效? 在ff中必须声明rel属性!
void 无效的, 空的; invalid: 无效的, void 和 invalid 在表示无效的时候, 是一样的, 等同的 the treaty (条约) was declared invalid ...
- 【bzoj1706】[usaco2007 Nov]relays 奶牛接力跑
题意 给出一张无向图,求出恰巧经过n条边的最短路. 题解 考虑先离散化,那么点的个数只会有202个最多.于是复杂度里面就可以有一个\(n^3\).考虑构造矩阵\(d^1\)表示经过一条边的最短路,那么 ...
- 【Hadoop 分布式部署 一 :分布式部署准备虚拟机 】
一.将IP配置为静态 按照 下面的操作将IP配置为静态IP 这个静态的IP地址 是你自己设置的,只要符合虚拟机的IP段就可以.最后点击 Apply 需要root密码 将网络断开 (在网络图标左键 ...
- EPPlus实战篇——Excel读取
.net core 项目 可以从excel读取任何类型(T)的数据,只要T中的field的[Display(Name = "1233")]中的name==excel column ...
- Git 提交的正确姿势:Commit message 编写指南
http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html Git 每次提交代码,都要写 Commit message( ...