16. 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).
如上例, -1 + 1 = 0 不也接近 1 吗?不行,因为人家要求3数之和.
思路同15题 3Sum. 既固定i, 让 lo = i + 1; hi = len - 1;
关键是对sum == ta, sum > ta, sum < ta的处理,以及退出while(lo < hi)之后的处理.
人家想法,咱代码:
\(O(n^2)\) time, \(O(1)\) extra space.
int threeSumClosest(vector<int>& A, int ta) {
const int n = A.size();
int min = INT_MAX; // 存放最接近ta的三个数和
sort(A.begin(), A.end());
for (int i = 0; i < n - 2; i++) {
// 剔除连续值
if (i > 0 && A[i] == A[i - 1]) continue;
int lo = i + 1, hi = n - 1;
// lo==hi 不可以,sum = A[i] + A[lo] + A[hi]会出问题
while (lo < hi) {
int sum = A[i] + A[lo] + A[hi];
// 保存最接近ta的3数之和
min = abs(sum - ta) < abs(min) ? (sum - ta) : min;
if (sum == ta) return ta;
else if (sum > ta) hi--;
else lo++;
}
}
return min + ta; // min = sum - ta, we ask sum now!
}
16. 3Sum Closest(中等)的更多相关文章
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- 15. 3Sum、16. 3Sum Closest和18. 4Sum
15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- 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 ...
随机推荐
- ELK学习总结(2-2)单模式CRUD操作
------------------------------------------------------ 1.查看索引信息 请求命令: GET /library/_settings GET /li ...
- linux下的Shell编程(6)case和select
第一个,除了if语句之外,Shell Script中也有类似C语言中多分支结构的case语句,它的语法是: case var in pattern 1 ) - ;; pattern 2 ) - ;; ...
- R数据分析 第一篇:温习概率论
概率论是人们在长期实践中发现的理论,是客观存在的.自然界和社会上发生的现象是多种多样的,有一类现象,在一定条件下必然发生,称作确定性现象,而概率论研究的现象是不确定性现象,嗯嗯,醒醒,概率论研究的对象 ...
- Java-Maven(七):Eclipse中Maven依赖、聚合、继承特性
之前通过学习了解,maven集成到eclipse中的如何创建项目,以及maven命令插件在eclipse中安装后的用法.那么接下来我们将会学习一些maven在项目中的一些特性,及如何使用. Maven ...
- POJ-3641 Pseudoprime numbers---快速幂
题目链接: https://vjudge.net/problem/POJ-3641 题目大意: 问p是不是伪素数.伪素数条件:①p不是素数.② ap = a (mod p). 思路: 直接快速幂模板+ ...
- 南阳OJ-6-喷水装置(一)
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=6 题目大意: 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷 ...
- JVMGC机制
GC 是JVM的垃圾回收器.与C/C++不同,java程序员无需考虑太多内存分配的位置,更不用考虑内存释放的机制,java对象内存的申请和释放都有JVM托管.JVM的内存释放机制就是GC. GC的过程 ...
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- 机器学习技法:13 Deep Learning
Roadmap Deep Neural Network Autoencoder Denoising Autoencoder Principal Component Analysis Summary
- [HNOI 2009]梦幻布丁
Description 题库链接 维护一个序列 \(A\) .支持以下操作: \(X~Y\) 将序列中所有的 \(X\) 变成 \(Y\) : 询问序列颜色段数. \(1\leq n,m\leq 10 ...