Leetcode 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
给一个整型数组,找出三个数使其和尽可能的接近给定的数,返回三个数的和
注意题目有个假设对于每个输入都有一个解决方法,与3Sum差不多
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
sort(num.begin(),num.end());
int closet = num[]+num[]+num[];
for(int i = ; i < num.size()-; ++ i){
int start = i+, end = num.size()-,sum = ;
while(start < end){
sum = num[i] + num[start] + num[end];
if(sum == target) return sum;
else if(sum > target) end--;
else start++;
closet = abs(sum - target) < abs(closet-target) ? sum : closet;
}
}
return closet;
}
};
Leetcode 3Sum Closest的更多相关文章
- [LeetCode]3Sum Closest题解
3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...
- [LeetCode] 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 3Sum Closest (Two pointers)
题意 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- LeetCode——3Sum Closest
Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- leetcode 3Sum Closest python
class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List ...
- LeetCode 3Sum Closest 最近似的3sum(2sum方法)
题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- 3Sum Closest - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...
随机推荐
- [Unity3d]3D项目转换为VR项目(暴风魔镜SDK)
使用暴风魔镜SDK来操作 将魔镜的摄像头拖放到项目中: 将MoJingVrHead的Script剪切到CamRoot中: 这个时候能看到显示2个物体了,不过使用的Canvas还是显示一个: 调整Can ...
- C# GUID转换成16位字符串或19位数字并确保唯一
/// <summary> /// 根据GUID获取16位的唯一字符串 /// </summary> /// <param name=\"guid\" ...
- Login Reference for PhotoSomething
Android Background Processing with Handlers and AsyncTask and Loaders - Tutorial http://www.vogella. ...
- JavaScript 代码风格指南
一.基本格式 缩进 建议每级4个空格,可以给编辑器设置tab = 4个空格,自动转换 分号 不要省略分号,防止ASI(自动插入分号)错误 行宽 每行代码不超过80个字符,过长应该用操作符手动断行 断行 ...
- 告别我的OI生涯
本文章写于2008年12月15日. 随着2008noip的结束,我也结束了我的OI生涯. 信息竞赛也许是从小到大让我最最努力的一件事.我记得参加2006noip初赛前,每天中午为了上信息课都吃不上中午 ...
- gcc 使用
参数 -E 只进行预处理-S 预处理和编译,生成汇编文件-c 预处理,编译和汇编,生成目标文件-g ...
- nfs部署和优化
nfs--网络文件系统 1.说明:允许一个系统在网络上与他人共享目录和文件 2.好处:通过nfs服务,就可以让这个机器访问远程的文件,像访问自己的文件一样,属于cs通信 3.原理说明:假设有A,B ...
- MySQL 5.5安装记录
安装gnake ./configure && gmake && gmake install 安装ncurses-devel yum install -y ncurses ...
- vs2010 在64bits系统下编译
vs只有32bits的没有64bits. 本来程序没问题,后面一直报错 LIBCMTD.lib(close.obj) : error LNK2001 LNK2019 换了各种 MD/MT/MTD 都没 ...
- Spring实例化Bean的三种方式及Bean的类型
1.使用类构造器实例化 [默认的类构造器] <bean id=“orderService" class="cn.itcast.OrderServiceBean"/ ...