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 ...
随机推荐
- poj1191 棋盘分割【区间DP】【记忆化搜索】
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16263 Accepted: 5812 Description ...
- c# 公共方法
MyMeans using System; using System.Collections.Generic; using System.Text; using System.Windows.Form ...
- 用Oracle实现ASH的数据透视图
11g里面有个很有用的新特性,对数据透视图的支持. 简单而言,它可以实现宽表和窄表之间的转化.举一个例子,有一张表记录了全校所有班级所有学生的成绩(A,B,C,D,E),现在想统计每个班级里每个分数级 ...
- HDU1556:Color the ball(简单的线段树区域更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定 ...
- SpringData_PagingAndSortingRepository接口
该接口提供了分页与排序功能 Iterable<T> findAll(Sort sort); //排序 Page<T> findAll(Pageable pageable); / ...
- Thymeleaf使用说明
Thymeleaf使用说明 javascript操作: a.<script type="text/javascript" th:inline="javascript ...
- bat笔记
背景介绍 现入职的公司包含发送EDM的项目,每天都有各种题型邮件需要发送,但是由于各种原因,发送EDM程序的服务器老是被网管各种重启 :) 作为负责人,对这事很恼火,隔几天就被投诉,怎么又没收到考勤邮 ...
- HDU 1879 继续畅通工程(Prim||Kruscal模板题)
原题链接 Prim(点归并) //异或运算:相同为假,不同为真 #include<cstdio> #include<algorithm> #define maxn 105 us ...
- 《FontForge常见问题FAQ》字王翻译版
<FontForge常见问题FAQ> 字王翻译版 原文: http://fontforge.github.io/en-US/faq/ 翻译: 字王·中国 blog: http://bl ...
- 反射调用发生错误信息 LoadNeither
错误信息: Service cannot be started. System.Reflection.TargetInvocationException: Exception has been thr ...