Title :

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).
int threeSumClosest(vector<int> &num, int target){
sort(num.begin(),num.end());
int min = INT_MAX;
int result;
for (int i = ; i < num.size()-; i++){
if (i > && num[i] == num[i-])
continue;
int left = i+;
int right = num.size()-;
int goal = target - num[i];
while (left < right){
int diff = abs(target - num[left] - num[right] - num[i]);
if (diff < min){
min = diff;
result = num[left] + num[right] + num[i];
}
if (num[left] + num[right] < goal){
while (left < right && (num[left] == num[left+]))
left++;
left++;
}else if (num[left] + num[right] > goal){
while (left < right && (num[right] == num[right-]))
right--;
right--;
}else{
return target;
}
}
}
return result;
}

LeetCode: 3SumClosest的更多相关文章

  1. [Leetcode] 3sum-closest 给定值,最为相近的3数之和

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

  2. LeetCode 3sum-closest 题解

    思路 排序 枚举一个数a 双指针移动法确定b和c 求和,更新最接近的值 复杂度 T(n)=O(n2)&ThickSpace;M(n)=O(1)T(n)=O(n^2) \; M(n)=O(1)T ...

  3. leetcode 日记 3sumclosest java

    思路一为遍历: public int thirdSolution(int[] nums, int target) { int result = nums[0] + nums[1] + nums[2]; ...

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

  5. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  6. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  7. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  8. <转>LeetCode 题目总结/分类

    原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...

  9. LeetCode (13): 3Sum Closest

    https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...

随机推荐

  1. 【ACMER纷纷表示】女生应该找一个玩ACM的男生

    1.强烈的事业心 将来,他也一定会有自己热爱的事业.而且,男人最性感的时刻之一,就是他专心致志做事的时候.所以,找一个机会在他全神贯注玩ACM的时候,从侧面好好观察他,你就会发现我说的话没错.2.永不 ...

  2. 01-01-01【Nhibernate (版本3.3.1.4000) 出入江湖】配置文件

    默认配置文件名称是:hibernate.cfg.xml 放置在应用程序集的根目录下 <?xml version="1.0" encoding="utf-8" ...

  3. html添加keyword,description帮助百度收录处理方法,jsp去除空白行方法

    1.将网页的title,keyword,description写成include包含文件,例如: top.jsp <%@ page language="java" conte ...

  4. java.util.ResourceBundle

    转载自: http://lavasoft.blog.51cto.com/62575/184605 这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以:          轻松地本地化或翻译 ...

  5. 0到N数其中三个数的全排列

    #include<iostream> using namespace std; int main(){ ; int count; count=; ;i<=N;i++) ;j<= ...

  6. java基础知识回顾之javaIO类--File类应用:递归深度遍历文件

    代码如下: package com.lp.ecjtu.File.FileDeepList; import java.io.File; public class FileDeepList { /** * ...

  7. 2010年山东省第一届ACM大学生程序设计竞赛 Balloons (BFS)

    题意 : 找联通块的个数,Saya定义两个相连是 |xa-xb| + |ya-yb| ≤ 1 ,但是Kudo定义的相连是 |xa-xb|≤1 并且 |ya-yb|≤1.输出按照两种方式数的联通块的各数 ...

  8. CF 221div2 A. Lever

    A. Lever 题目:http://codeforces.com/contest/376/problem/A 题意:杠杆原理 比两边的重量 input =^== output balance 9== ...

  9. 220 DIV2 B. Inna and Nine

    220 DIV2 B. Inna and Nine input 369727 output 2 input 123456789987654321 output 1 题意:比如例子1:369727--& ...

  10. cojs 简单的数位DP 题解报告

    首先这道题真的是个数位DP 我们考虑所有的限制: 首先第六个限制和第二个限制是重复的,保留第二个限制即可 第五个限制在转移中可以判断,不用放在状态里 对于第一个限制,我们可以增加一维表示余数即可 对于 ...