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 ...
随机推荐
- maven快速下载jar镜像
<!--国内镜像--><mirror> <id>CN</id> <name>OSChina Central</name> ...
- FatMouse's Speed ~(基础DP)打印路径的上升子序列
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...
- s遇到错误不要慌,教你方法走四方
我觉得不管是新手还是老手,他们都会出错,有些错误控制台会报错,而有些错误控制台不会报错 面对不会报错的时候,就有一些人烦恼,不知道怎么办了,久而久之,就失去了对学习的乐趣. 所以我在这里说一下对错误处 ...
- 一个适用于单页应用,返回原始滚动条位置的demo
如题,最近做一个项目时,由于页面太长,跳转后在返回又回到初始位置,不利于用户体验,需要每次返回到用户离开该页面是的位置.由于是移动端项目,使用了移动端的套ui框架framework7,本身框架的机制是 ...
- Python学习之list有序集合
# coding=utf-8 # list有序集合 classmate = ['Michael', 'Bob', 'Tracy'] print classmate print len(classmat ...
- .net core 使用阿里云短信发送SMS
阿里云官方的skd(aliyun-net-sdk-core,aliyun-net-sdk-dysmsapi)在dnc中发送短信会出错,nuget上的包貌似也一样不管用.直接改下sdk当然也可以,但就发 ...
- hdu1072 Nightmare---BFS
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意: 在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重 ...
- NGUI----简单聊天系统一
1:聊天背景的创建 新建一个场景-----保存场景 NGUI---->Create-----Panel 选中UIRoot,然后新建一个sprite 选择图集 效果如下图 添加一个可拖拽的功能 选 ...
- IOS 中openGL使用教程4(openGL ES 入门篇 | 离屏渲染)
通常情况下,我们使用openGL将渲染好的图片绘制到屏幕上,但有时候我们不想显示处理结果,这时候就需要使用离屏渲染了. 正常情况下,我们将屏幕,也就是一个CAEAGLLayer对象作为渲染目标,离屏渲 ...
- SpringBoot: 配置加载顺序
在应用程序中各种配置是不可避免的,Spring中对配置的抽象(Environment)可以说是达到了极致,其中有一项尤为突出:PropertySource(配置来源),配置来源通常包括命令行参数,系统 ...