https://leetcode.com/problems/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).

思路:

和上一道差不多,主要还是有序数组里两个flag移动。

AC代码:

class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int res,t,j,k,lowerbound=INT_MAX,n=nums.size();
sort(nums.begin(),nums.end());
for(int i=;i<n-;i++){
t=target-nums[i];
j=i+;
k=n-;
while(j<k){
if(abs(nums[j]+nums[k]-t)<lowerbound){
res=nums[i]+nums[j]+nums[k];
lowerbound=abs(nums[j]+nums[k]-t);
}
if(nums[j]+nums[k]<t)
j++;
else
k--;
}
}
return res;
}
};

LeetCode(16)题解--3Sum Closest的更多相关文章

  1. LeetCode(15)题解--3Sum

    https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c i ...

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

  3. leetcode第16题--3Sum Closest

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

  4. LeetCode题解——3Sum Closest

    题目: 给定一个数组,和一个指定的值,找出数组中3个数,它的和最接近这个指定值,并返回这个和. 解法: 和上一题找3个数的和为0一样,先排序再遍历,这一次不需要记录路径. 代码: class Solu ...

  5. LeetCode OJ:3Sum Closest(最接近的三数之和)

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

  6. 【LeetCode】016 3Sum Closest

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

  7. leetcode笔记:3Sum Closest

    一.题目描写叙述 二.解题技巧 该题与3Sum的要求类似.不同的是要求选出的组合的和与目标值target最接近而不一定相等.但实际上,与3Sum的算法流程思路类似,先是进行排序.然后顺序选择数组A中的 ...

  8. 《LeetBook》leetcode题解(16):3Sum Closest [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

随机推荐

  1. 加速和简化构建Docker(基于Google jib)

    赵安家 2019年02月11日阅读 1518 关注 加速和简化构建Docker(基于Google jib) 介绍 其实jib刚发布时就有关注,但是一直没有用于生产,原因有二 基于 spotify/do ...

  2. zoj 3791 An Easy Game dp

    An Easy Game Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Edward and Flandre play a ga ...

  3. Pushlets的初始化陷阱

    Pushlets是在类名为Pushlet的servlet的init方法中进行初始化的.一般我们会在web.xml配置pushlet的时候,指定其servlet在Web应用启动时就进行初始化,即便这样, ...

  4. luogu 1142 轰炸 最多共线点数

    题目链接 题意 给定\(n(n\leq 700)\)个点,问共线的点最多有多少个? 思路 \(O(n^3)\):枚举两个顶点确定一条直线,再看有多少个顶点在这条直线上.讲道理会T. \(O(n^2lo ...

  5. 视音频数据处理入门:RGB、YUV像素数据处理【转】

    转自:http://blog.csdn.net/leixiaohua1020/article/details/50534150 ==================================== ...

  6. Linux 之 文件压缩解压

    文件压缩解压 参考教程:[千峰教育] 命令: gzip: 作用:压缩文件,只能是单个文件,不能是多个,也不能是目录. 格式:gzip file 说明:执行命令会生成file.gz,删除原来的file ...

  7. Codeforces Gym101502 A.Very Hard Question

    2017 JUST Programming Contest 3.0 昨天的训练赛,打的好难过,因为被暴打了,写了8题,他们有的写了9题,差了一道dp,博客上写7道题的题解. 因为有一道是套板子过的,并 ...

  8. 咦?Oracle归档文件存哪了?

    实验环境:RHEL 5.4 + Oracle 11.2.0.3 现象:日志切换后没找到归档日志目录. 1.查看归档日志路径 2.日志切换后并未找到归档目录 3.创建归档目录后再次观察 引申知识 1.查 ...

  9. 洛谷——P2196 挖地雷

    题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径.当地窖及其连接的数据给出之后,某人可以从任一处 ...

  10. 几点iOS开发技巧

    转自I'm Allen的博客   原文:iOS Programming Architecture and Design Guidelines   原文来自破船的分享   原文作者是开发界中知晓度相当高 ...