[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 ...
随机推荐
- html中文字溢出处理(text-overflow)
文字溢出处理有两种方式: 一.css overflow:hidden; white-space: nowrap; text-overflow: ellips ...
- redis cluster 的ERR max number of clients reached 问题排查
早上发现微服务连不上redis cluster了,看来下日志如下 [root@win-jrh378d7scu 7005]# bin/redis-cli -c -h 15.31.213.183 -p 7 ...
- linux中创建一个回收站
1. mkdir /tmp/trash_tmp 建立一个回收站目录 2. vi /bin/trash 编辑一个文件 mv $@ /tmp/trash_tmp :wq 保存退出 3. ...
- jmeter - jp@gc - Active Threads Over Time(多台负载用户)
问题: 线程数设置:30,远程启动2台机子 查看 jp@gc - Active Threads Over Time图,发现只统计了1台机子的线程数,线程数并不是60: 解决办法: 官方文档中提到: 1 ...
- MySQL数据库的账户管理
账户管理 在生产环境下操作数据库时,绝对不可以使用root账户连接,而是创建特定的账户,授予这个账户特定的操作权限,然后连接进行操作,主要的操作就是数据的crud MySQL账户体系:根据账户所具有的 ...
- HAL库PWM
1.占空比控制 a.在CUBMX的TIM下选择时钟源,选择通道为模式PWM Generation ch1 ,设置分频系数,初始值,不需要自动重装载,选择PWM模式1或2,设置比较值pulse,设置初始 ...
- [温故]图解java多线程设计模式(二)
join & interrupt 这俩方法属于线程对象里的方法,属于线程本身的操作. join方法 用于等待一个线程的终止,等待期间将会阻塞,直到被等待的线程终止结束. 所以join可以用来做 ...
- Q312 戳气球
有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[ ...
- Android:刚6瓶啤酒4两56度白酒下肚,居然20分钟做了一手机版网站 !
刚6瓶啤酒4两56度白酒下肚,居然20分钟不到时间做了一手机版网站 !人有多大潜力你知道吗? 大家有兴趣的可以用手机或微信打开 http://xh.yunxunmi.com/ 看看俺这酒后之做! 更 ...
- 完全国人自主研发原创的智能软件路由器BDS即将发布,附带企业服务总线ESB功能
完全国人自主研发原创的智能软件路由器即将发布: 完全国人自主研发原创的智能软件路由器BDS即将发布,附带企业服务总线ESB功能 智能软件路由器 BDS 简要介绍 http://kan.weibo.co ...