Leetcode 16
//一次AC 有点爽的
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int res = ;
int minal = INT_MAX;
sort(nums.begin(),nums.end());
for(int i=;i < nums.size()-;i++){
int cnt = target-nums[i];
for(int j=i+,k=nums.size()-;j < k;){
int sum = nums[j]+nums[k];
if(sum < cnt){j++;}
else if(sum > cnt){k--;}
else{
res = target;
return res;
}
if(minal > abs(sum-cnt)){minal = abs(sum-cnt);res = sum+nums[i];}
}
}
return res;
}
};
Leetcode 16的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- 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 ...
- LeetCode——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...
- LeetCode 16 3Sum Closest (最接近target的3个数之和)
题目链接 https://leetcode.com/problems/3sum-closest/?tab=Description Problem : 找到给定数组中a+b+c 最接近targe ...
- LeetCode(16)题解--3Sum Closest
https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers ...
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- 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 ...
- 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 ...
随机推荐
- 南京网络赛E-AC Challenge【状压dp】
Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answe ...
- ES6基础教程(整理自阮一峰)
------------------------ECMAScript 6 简介------------------------ECMAScript 和 JavaScript 的关系是,前者是后者的规格 ...
- Python性能分析指南(未完成)
英文原文:http://www.huyng.com/posts/python-performance-analysis/ 译文:http://www.oschina.net/translate/pyt ...
- sql server 2008维护计划配置
转自:https://blog.csdn.net/yunye114105/article/details/6594826 一:目标任务: 使用SQL Server 2008自带的维护计划自动备份数据库 ...
- 详谈LABJS按需动态加载js文件
为了提高页面的打开和加载速度,我们经常把JS文件放在页面的尾部,但是有些JS必须放在页面前面,这样就会增加页面的加载时间:于是出现了按需动态加载的概念,这个概念就是当页面需要用到这个JS文件或者CSS ...
- Sparsity稀疏编码(三)
稀疏编码(sparse coding)和低秩矩阵(low rank)的区别 上两个小结介绍了稀疏编码的生命科学解释,也给出一些稀疏编码模型的原型(比如LASSO),稀疏编码之前的探讨文章 ...
- SQL SERVER 2008 R2序列号
SQL SERVER 2008 R2序列号: 数据中心版:PTTFM-X467G-P7RH2-3Q6CG-4DMYB 开 发者 版:MC46H-JQR3C-2JRHY-XYRKY-QWPVM 企 ...
- 向Oracle 数据表中插入一条带有日期类型的数据
有一张表:batch(批次表) 表的字段如下: 第一种情况: 现在需要插入一条当前的系统时间 sql 如下: insert into batch (batch_id, cus_id, batch_nu ...
- java 读CSV 和 Excel
1.csv和excel读写对比 开发中经常遇到数据导入和导出功能,csv 和 excel是最常见的数据格式,本文比较了下csv和excel读写相同数据的效率: 测试数据格式一 用上面模板数据生成的测试 ...
- 20145329 《网络对抗技术》MS08_067远程漏洞攻击
MS08_067远程漏洞攻击:shell 实现攻击的前提是:攻击机和靶机在同一个网段下,首先将kali的ip改为与winxp的ip一样,二者能ping通 两台虚拟机: kali ip:192.168. ...