LeetCode16 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. (Medium)
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).
分析:
排序、遍历方式顺序 与3 sum思路一致,维护一个与target的差值即可。
代码:
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int diff = 0x7FFFFFFF;
for (int i = ; i < nums.size() - ; ++i) {
int start = i + , end = nums.size() - ;
while (start < end) {
int curSum = nums[i] + nums[start] + nums[end];
if (curSum - target == ) {
return target;
}
else if (curSum - target > ) {
if (curSum- target < abs(diff) ) {
diff = curSum - target;
}
end--;
}
else {
if (target - curSum < abs(diff)) {
diff = curSum - target;
}
start++;
}
}
}
return diff + target;
}
};
LeetCode16 3Sum Closest的更多相关文章
- [array] leetCode-16. 3Sum Closest -Medium
16. 3Sum Closest -Medium descrition Given an array S of n integers, find three integers in S such th ...
- Leetcode16.3Sum Closest最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 2Sum,3Sum,4Sum,kSum,3Sum Closest系列
1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...
- [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 ...
随机推荐
- dom 封装表单控件
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- javascript里面dom操作和兼容问题汇总
DOM 增,删,改,查 oUl.children 获取UL的子节点 有length的属性 oUl.children[0] 获取UL第1个子节点 使 ...
- Sharding & IDs at Instagram(转)
英文原文:http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram 译文:http://ww ...
- uber优步提高成单率,轻松拿奖励!
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Wicket Hello World Example
A simple hello world example in Wicket, show the basic structure of Wicket web application. Tools an ...
- Java中的多线程操作初探
问题引出: 说是java,其实还是在做android的时候遇到的问题,在android 4.0以后,访问网络必须在新线程中实现,所以才会遇到这个问题.只是为了方面说明问题,才新建一个java项目.在m ...
- 关于dll的路径问题
最近在做一个sdk二次开发的项目,具体是将一个C++开发的SDk用C#将它的API接口全部封装一遍,然后再做一个demo就好了 好不容易封装完了,在使用的时候出了问题.原来SDK中的dll老是加载不到 ...
- UVa 10004:Bicoloring
这道题要我们判断所给图是否可以用两种颜色进行染色,即"二染色“.已知所给图一定是强连通图. 分析之: 若图中无回路,则该图是一棵树,一定可以二染色. 若图中有回路,但回路有偶数个节点,仍然可 ...
- android开发中提示:requires permission android.permission write_settings解决方法
一.在Manifest.xml 中添加: <uses-permission android:name="android.permission.WRITE_CONTACTS" ...
- Android 模仿电视机关闭界面
Tween动画 1.在关闭电视机的时候,电视机中间都有一根白条瞬间关闭. 要实现这个效果其实就是利用Tween动画进行实现的. 动画的xml 文件是: android:startOffset=&quo ...