[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).
这道题的解题思路是比较常规的。首先将数组排序,设置三个下标first、second、third分别初始化为0,1,nums.size()-1。
1、如果这三个下标对应的数据之和大于target,那么就把third减一,
2、如果小于target,就把second加一。
3、如果等于target,结束查找,返回这个值。
每当second和third相遇的时候就让first+1,重置second = first + 1,thrid = nums.size() - 1。
这样就可以保证扫描到所有可能的结果,并且算法复杂度为O(n^2).
代码如下:
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
if(nums.size()<3) return 0;
int first = 0, second = 1, third = nums.size() - 1;
sort(nums.begin(),nums.end());
int re = nums[0] + nums[1] + nums[2];
for(;first < nums.size()-2;first++){
second = first + 1;
third = nums.size() - 1;
while(second < third){
int temp = nums[first] + nums[second] + nums[third];
if(abs(re - target) > abs(temp - target)) re = temp;
if(temp == target){
return temp;
}
else if(temp < target){
second ++;
}
else{
third --;
}
}
}
return re;
}
};
[LeetCode]3Sum 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
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, ...
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 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 ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
随机推荐
- 使用Fiddler代理调试本地手机页面
从事前端开发的同学一定对 Fiddler 不陌生,它是一个非常强大的http(s)协议分析工具.我们知道如何在电脑上调试页面请求,但在手机端你没有这么多强大好用的调试工具来调试你的webapp,如果你 ...
- Nodejs Express模块server.address().address为::
来自 http://blog.csdn.net/medivhq/article/details/74171939 我按照菜鸟教程上的写法为:(http://www.runoob.com/nodejs/ ...
- elasticsearch-analysis-pinyin
来源:https://github.com/medcl/elasticsearch-analysis-pinyin Pinyin Analysis for Elasticsearch This Pin ...
- 用Yeoman构建AngularJS项目
转至https://yq.aliyun.com/articles/25578 Yeoman 介绍 Yeoman 是 Google 的团队和外部贡献者团队合作开发的,他的目标是通过 Grunt(一个用于 ...
- 使用hexo+coding搭建免费个人博客
1.检测node和npm 先检测一下有没有node.js和npm $ node -v //如果有,说明node.js安装成功! $ node -v v8.4.0 //如果有,说明npm安装成功! $n ...
- 启动多个appium服务(同时运行多台设备)
准备: 一台真机一台模拟器(使用的是“夜神模拟器”) 先查看是否检测到设备 adb devices 由上图可看出没有检测到模拟器(夜神模拟器已开启) 可通过以下配置完成: 第一步:找到adb的 ...
- Webpack学习错误解决笔记
错误1:在用npm install 安装模块时,时常会出现没有以下类似的错误 解决方法:右键点击node_modules文件夹,选取属性,将文件夹只读选项去除 错误2:在学习到清理/dist文件夹这块 ...
- CF917D. Stranger Trees & TopCoder13369. TreeDistance(变元矩阵树定理+高斯消元)
题目链接 CF917D:https://codeforces.com/problemset/problem/917/D TopCoder13369:https://community.topcoder ...
- L: Long Long Ago---二分
L: Long Long Ago 时间限制: 1 s 内存限制: 128 MB 题目描述 今天SHIELD捕获到一段从敌方基地发出的信息里面包含一串被经过某种算法加密过的的序列 ...
- 正则表达式 IP域名
不废话,我这个起码不坑人,有的把我坑死 var objRegExp = /^((([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5]))))\.)((([0-9]|([ ...