16. 3Sum Closest -Medium

descrition

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 的思路一样。不同在于,我们现在希望找到距离 target 最近的数,参看代码。

code


#include <iostream>
#include <vector>
#include <algorithm>
#include <limits> using namespace std; class Solution{
public:
int threeSumClosest(vector<int>& nums, int target){
sort(nums.begin(), nums.end()); // ascending int min_gab = numeric_limits<int>::max();
int ans = target; for(int i=0; i<nums.size(); i++){
int target_local = target - nums[i];
int ileft = i + 1;
int iright = nums.size() - 1;
while(ileft < iright){ // two pointer searching
int sum = nums[ileft] + nums[iright];
if(sum == target_local) // right answer
return target;
if(sum < target_local) // move ileft to increase sum
ileft++;
else // sum > target_local
iright--; int gab = abs(sum - target_local);
if(gab < min_gab){
ans = sum + nums[i];
min_gab = gab;
}
}
} return ans; }
}; int main()
{
return 0;
}

[array] leetCode-16. 3Sum Closest -Medium的更多相关文章

  1. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  2. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  3. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

    题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  4. Array + two points leetcode.16 - 3Sum Closest

    题面 Given an array nums of n integers and an integer target, find three integers in nums such that th ...

  5. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

  10. [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 ...

随机推荐

  1. jQuery插件--根据数据加载的进度动画案例

    css: *{ margin:; padding:; } @media screen and (min-width:320px){ html{font-size:12px;}} @media scre ...

  2. Spring中提供的集合工具类util CollectionUtils

    转自:https://blog.csdn.net/fangwenzheng88/article/details/78457850 CollectionUtils类 /* * Copyright 200 ...

  3. ReactJs 入门DEMO(转自别人)

    附件是分享的一些他人的ReactJs入门DEMO,以前版本使用的是JSXTransformer.js,新版的用browser.min.js替代了. DEMO 下载地址:http://files.cnb ...

  4. MySQL 汉字拼音

    http://blog.csdn.net/u012076316/article/details/54951365 http://www.cnblogs.com/diony/p/5483108.html ...

  5. android开发者要懂得问题答案

    我在网上看了一下有些人在博客上提出一些什么android开发者必须懂得问题,可是就是没有答案,所以我就把这些问题拷贝过来了.顺便也把全部的答案加上,为了让很多其它的开发者高速的找到答案,谢谢! 以下的 ...

  6. eclipse-hierarchyviewer 不能使用

    今天安装了adt-bundle以后,发现hierarchyviewer不能用.点开了以后连手机没有效果.后来发现,还需要进入hierarchyviewer所在的sdk目录进行下权限的设置 chmod ...

  7. TextView-shadow 阴影实现

    直接上代码 1)实现普通效果 <TextView android:layout_width="match_parent" android:layout_height=&quo ...

  8. winform程序,备份数据库+并压缩+并删除以前的备份

    说明:为了定时备份服务器上的数据库并压缩到指定目录,方便下载到本地而写本程序.配合windows的任务计划,可以达到定时备份数据库的目的. 程序需引用SQLDMO.DLL,如电脑上已安装sqlserv ...

  9. ORACLE11g R2【RAC+ASM→RAC+ASM】

    ORACLE11g R2[RAC+ASM→RAC+ASM] 本演示案例所用环境:RAC+ASM+OMF   primary standby OS Hostname node1,node2 dgnode ...

  10. java与javax的区别分析

    Java是一种受C语言影响的编程语言.Java和Javax本质上是与Java编程语言的上下文一起使用的包.实际上Java和Javax没有区别.这只是不同的名字. Java是一种编程语言,受到C语言的影 ...