做了几周的hard之后,这道题居然轻易就解出来了,稍微debug了一下就ac了,算是有了一丢丢提高把;

思路

这道题因为和三数之和很像,所以充分利用双指针的思想;先排序,然后再固定一个数i,i取值从【0,n-i】,
然后对r=i+1,l=n-1利用双指针来找最近的数

下面是直接应用abs 和sort库函数的代码,头文件 < algorithm > or <bits/stdc++.h>

class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
//time O(n^2), space O(1);
int n=nums.size();
if(n<3) return -1;
int res=nums[0]+nums[1]+nums[2];
sort(nums.begin(),nums.end()); for(int i=0;i<=n-3;i++){
int l=i+1,r=n-1; while(l<r){
int tmp_sum=nums[i]+nums[l]+nums[r];
if(tmp_sum>target) r--;
else l++;
if(abs(tmp_sum-target)<abs(res-target)) res=tmp_sum;
}
}
return res;
}
};

自己写快速排序qsort 和lambda表达式(在此类似于内联函数) f_abs的代码:

class Solution {
public: void qsort(vector<int>&nums, int start, int end){
if(start>=end) return; //partition block;
int pivot=nums[start];
int l=start,r=end;
while(l<r){
while(l<r && nums[r]>=pivot)
r--;
nums[l]=nums[r];
while(l<r && nums[l]<=pivot)
l++;
nums[r]=nums[l];
}
nums[l]=pivot;
pivot=l; //recursion block;
qsort(nums,start,pivot-1);
qsort(nums,pivot+1,end);
}
int threeSumClosest(vector<int>& nums, int target) {
//time O(n^2), space O(1);
int n=nums.size();
if(n<3) return -1;
int res=nums[0]+nums[1]+nums[2];
qsort(nums,0,n-1); auto f_abs = [&](int num){return num>0?num:(-num);};
for(int i=0;i<=n-3;i++){
int l=i+1,r=n-1; while(l<r){
int tmp_sum=nums[i]+nums[l]+nums[r];
if(tmp_sum>target) r--;
else l++;
if(f_abs(tmp_sum-target)<f_abs(res-target)) res=tmp_sum;
}
}
return res;
}
};

leetcode16 最接近的三数之和的更多相关文章

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

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

  2. LeetCode16.最接近的三数之和 JavaScript

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

  3. [Swift]LeetCode16. 最接近的三数之和 | 3Sum Closest

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  4. leetcode16 最接近的三数之和 双指针

    三个数循环太复杂 确定一个数,搜索另两个 先排序,之后就确定了搜索的策略 if(tp>target) while (l < r && nums[r] == nums[--r ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 3.web开发入门知识

    /*web入门*/ /*互联网上常用的协议以及它的端口*/ http 80 http://localhost/    相当于    http://localhost:80/         http协 ...

  2. Linux磁盘的管理

    文件系统 磁盘必须要有文件系统---数据库 文件系统是用来数据存储,数据库是用来管理数据 windows fat32  ntfs   exfat linux  单文件系统 inode--索引空间(文件 ...

  3. 小程序Flex布局

    容器属性 容器支持的属性有:display:通过设置display属性,指定元素是否为Flex布局.flex-direction:指定主轴方向,决定了项目的排列方式.flex-wrap:排列换行设置. ...

  4. 微信小程序双向绑定

    欢迎加入前端交流群交流知识获取视频资料:749539640 vue.angular的双向绑定如下示例: <div> <input type="text" [(ng ...

  5. java——多线程并发库

    JDK5中增加了Doug Lea的并发库,这一引进给Java线程的管理和使用提供了强大的便利性. java.util.current包中提供了对线程优化.管理的各项操作,使得线程的使用变得的心应手.该 ...

  6. vue-cli3项目中使用CDN

    1.html 中引入 echarts         html中添加script标签如下:         <script src="//cdn.bootcss.com/echarts ...

  7. mongodb的安装与使用(三)之 pymongo

    (一)连接MongoClient 连接MongoDB我们需要使用PyMongo库里面的MongoClient,一般来说传入MongoDB的IP及端口即可,第一个参数为地址host,第二个参数为端口po ...

  8. HDU 5634 (线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5634 题意:给出 n 个数,有三种操作,把区间的 ai 变为 φ(ai):把区间的 ai 变为 x:查 ...

  9. 无法安装 Composer 怎么办?

    详细方法参见 http://pkg.phpcomposer.com/ 下载 composer.phar ,放置到项目中 在同目录中执行命令行 echo @php "%~dp0composer ...

  10. Elasticsearch7.1中文文档-第一章-入门

    安装openjdk wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-secur ...