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 ...
随机推荐
- Flow简易教程——安装篇
.mydoc_h1{ margin: 0 0 1em; } .mydoc_h1_a{ color: #2c3e50; text-decoration: none; font-size: 2em; } ...
- NHibernate优点和缺点:
NHibernate优点: 1.完全的ORM框架. NHibernate对数据库结构提供了较为完整的封装,它将数据库模式映射为较完全的对象模型,支持封装,继续机制,功能较强大,比一般的ORM灵活性高. ...
- 超简单的jQuery前台分页,不需导包
今天我们介绍一个不需要导分页包的,非常容易上手的分页+模糊查询功能.接下来先介绍分页功能: 首先第一步,你要有个要去分页的列表.我这里敲了个简单的图书管理,作为展示的基础,它的列表为异步提交,由两部分 ...
- python中的turtle库绘制图形
1. 前奏: 在用turtle绘制图形时,需要安装对应python的解释器以及IDE,我安装的是pycharm,在安装完pycharm后,在pycharm安装相应库的模块,绘图可以引入turtle模块 ...
- Python之socketserver模块和验证客户端链接的合法性
验证客户端链接的合法性 分布式系统中实现一个简单的客户端链接认证功能 #_*_coding:utf-8_*_ from socket import * import hmac,os secret_ke ...
- python 字符串 字节
字符串 字节 a. 字符串转字节 1 2 key = "xxxx" bkey = bytes(key,encoding='utf-8') b. bytearray 数组 1 2 ...
- win10下安装Ubuntu16.04双系统
其实我是不喜欢系统的,之前都是在win下面进行开发,现在来了个项目,经过各种环境的安装调研,最终选择在Ubuntu下面进行开发.之前想着为啥不在虚拟机里面安装Ubuntu进行操作呢,由于虚拟机的体验不 ...
- windows10无法启动承载网络
每个都试一下
- shell多进程脚本
#!/bin/bash python_path=/home/huaw/crawler python_name=list_all_v6_crawler.py MAX_SYNC_PROCESS=40 ec ...
- Canvas-自由绘制
#自由绘制 from tkinter import * master=Tk() c=Canvas(master,width=400,height=200) c.pack() def paint(eve ...