数组 -- 3Sum Closet -- 图解
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.
e sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution{
public:int threeSumCloest(vector<int>& num,int target){int result = 0;int min_gap = INT_MAX;sort(num.begin(),num.end());for(auto a = num.begin();a !=prev(num.end(),2); a = upper_bound(a,prev(num.end()),*a){auto b = next(a);auto c = prev(num.end());while(b < c){const int sum = *a + *b + *c;const int gap = abs(sum - target);if(gap < min_gap ) {result = sum ;min_gap = gap;}if(sum < target){b = upper_bound(b, c, *b);}else{c = prev(lower_bound(b, c, *c));} }}return result;}
};
lower_bound(a, b, val):返回容器中第一个值【大于或等于】val的元素的iterator位置。
upper_bound(a, b, val):返回容器中第一个值【大于】
参考资料:
LeetCode题解
数组 -- 3Sum Closet -- 图解相关推荐
- 3sum、3Sum closet、 4sum
1. 3Sum [题目] Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0 ...
- 【动态规划】01背包问题(滚动数组 + 手画图解)
01背包除了可以用形象的二维动态数组表示外,还可以使用空间复杂度更低的一维滚动数组. 目录 文章目录 前言 一.滚动数组的基本理解 二.确定dp及其下标含义 三.确定递推公式 四.确定初始化 五.确定 ...
- java 数组 内存_图解Java数组的内存分配
1. Java数组是静态的 Java是静态语言,所以Java的数组也是静态的,即:数组被初始化后,长度不可变 静态初始化:显式指定每个数组元素的初始值,系统决定数组长度 String[] books ...
- c语言数组的使用步骤,C语言中为什么要使用数组?详细图解数组的使用
唉,有这么多不同主题的书.在哪里才能找到有关中国音乐的书呢? 数组是可以在内存中连续存储多个元素的结构 数组中的所有元素必须属于相同的数据类型 1.在计算机内存中,同一数组的所有元素按下标顺序依次存放 ...
- [ 1001] 动态开辟二维数组的说明
开辟二维数组的说明 图解 [1][][][][] [2][][][][] [3][][][][] void main() { int i,j; /*注意申请的指针格式 先创建的是1,2,3首地址为 ...
- LeetCode -- 3Sum
Question: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? F ...
- [LeetCode]#13 3sum
一.题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- Java语言基础(数组)
Java语言基础(数组概述和定义格式说明) A:为什么要有数组(容器) 为了存储同种数据类型的多个值 B:数组概念 数组是存储同一种数据类型多个元素的集合.也可以看成是一个容器. 数组既可以存储基本数 ...
- java基数排序 数组_万字长文带你掌握Java数组与排序,代码实现原理都帮你搞明白!...
查找元素索引位置 基本查找 根据数组元素找出该元素第一次在数组中出现的索引 public class TestArray1 { public static void main(String[] arg ...
- [转载] JAVA笔记_(Day04,Day05)函数数组
参考链接: 了解Java中的数组IndexOutofbounds异常 文章目录 函数定义练习误区重载(overload)重载选择题练习函数的内存调用问题 数组定义数组的内存图解数组的常见问题应用求和最 ...
最新文章
- 两年的等待,近700页干货,同系列累计销量25万+,第三版内容更扎实
- lamp介绍,wordpress,phpmyadmin,discuzz安装
- 一个备份MySQL数据库的简单Shell脚本(转)
- PostProcessor
- html5 接收蓝牙广播_蓝牙定位技术浅析(化工厂应用)
- 两张趣图助你理解状态码的含义~
- java –cp_Java –缺少字体–崩溃的应用程序!
- 微软要干掉ADOBE?
- batocera游戏整合包_FIFAol3头像包整合
- 常用 linux 快捷键设置,Eclipse常用的快捷键及配置
- Xml中SelectSingleNode方法,xpath查找某节点用法
- 人口各省预测模型matlab_利用matlab编程求解人口预测模型.doc
- 某丰快递分拣小程序-python字典
- matlab画空间分布图,matlab空间图形的画法.doc
- android辅助功能失效,如果其他应用的辅助功能服务处于开启状态,则Android无障碍服务无法正常工作...
- SMP、NUMA、MMP的简介
- 2020移动apn接入点哪个快_为什么别人的4g网总比你快?手机这个设置没开启,难怪网络...
- 程序设计思维与实践 Week2 作业 B - Pour Wate
- vim 退格键(backspace)
- IList和List解惑
热门文章
- L1-023 输出GPLT (20 分) — 团体程序设计天梯赛
- RK平台计算GPIO对应的整型数
- 覆盖的面积 HDU - 1255 (扫描线, 面积交)
- Eclipse 工程迁移到 Android Studio
- unity 企鹅砸小猪 笔记1
- Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
- java开发编写基本的结构_【自学java笔记#第五天#】Java的基本程序设计结构
- HCIE Security 防火墙反病毒 备考笔记(幕布)
- CentOS安装后初始配置
- Linux硬链接与软链接的区别