[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 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).
问题:给定一个数组和一个整数 n ,求数组中的三个元素,他们的和离 n 最近。
这道题和 3Sum 很相似,解题思路也很相似,先排序,然后采用双指针法依次比较。
由于 3Sum 很相似,思路就不详说。主要说下不同点:
- 由于是求最接近值,当 s[i] + s[l] + s[r] - target == 0 的时候,即可返回结果。
- 返回的结果是最接近 n 的三个元素之和。
int threeSumClosest(vector<int>& nums, int target) {
std::sort(nums.begin(), nums.end());
int closest = target - nums[] - nums[] - nums[];
for (int i = ; i < nums.size(); i++) {
int l = i + ;
int r = (int)nums.size() - ;
int newTarget = target - nums[i];
while (l < r) {
if (nums[l] + nums[r] == newTarget) {
return target;
}
int diff = newTarget - nums[l] - nums[r];
if (abs(diff) < abs(closest)) {
closest = diff;
}
if (nums[l] + nums[r] < newTarget){
l++;
}else{
r--;
}
}
}
return target - closest;
}
[LeetCode] 16. 3Sum Closest 解题思路的更多相关文章
- 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, ...
- Java [leetcode 16] 3Sum Closest
题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...
- leetcode 16. 3Sum Closest JAVA
题目: 给定一个包括n个整数的数组nums和一个目标值target.找到nums中的三个整数,使得他们之和与target最为接近.返回三个整数之和,假定每组输入只存在唯一答案 解题思路: 将nums数 ...
- [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 ...
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- Array + two points leetcode.16 - 3Sum Closest
题面 Given an array nums of n integers and an integer target, find three integers in nums such that th ...
- 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 ...
随机推荐
- js 中如何通过提示框跳转页面
通过提示框跳转页面 <!doctype html> <html lang="en"> <head> <meta charset=" ...
- canvas径向渐变详解
创建径向渐变步骤如下: 1,创建径向渐变对象 createRadialGradient(x0,y0,r0,x1,y1,r1),其中x0,y0,r0分别为起始圆的位置坐标和半径,x1,y1,r1为终止圆 ...
- MySQL事务隔离级别初探
MySQL有四种隔离级别,分别是: READ UNCOMMITTED(未提交读) READ COMMITTED(提交读) REPEATABLE READ (可重复读) SERIALIZABLE(可串行 ...
- delphi cxgrid导出excel去除货币符号
版本 : devexpress 13.1.4 打开 包在ExpressExportLibary目录中. 修改FCells.SetCellDataCurrency为FCells.SetCellData ...
- 如何在windows上安装部署设置SVN服务器
1 一.准备工作 1.SVN服务器:解压缩包,可以从官方网站下载最新版本. 2.SVN客户端:TortoiseSVN,即常说的小乌龟,是一个客户端程序,用来与服务器端通讯. 2 二.安装服务器和客 ...
- Java String.contains()方法(转载)
Java String.contains()方法 Java String.contains()方法用法实例教程, 返回true,当且仅当此字符串包含指定的char值序列 描述 java.lang.St ...
- [转]ef获取某个表中的部分字段值
我有个新闻表 id,title,body,createtime,author,click 使用ef4.1 仅仅读取 id,title,createtime 并显示在页面上. public static ...
- C++版 Chip8游戏模拟器
很早就想写个FC模拟器,但真是一件艰难的事情.. 所以先写个Chip8模拟器,日后再继续研究FC模拟器. Chip8只有35条指令,属于RISC指令集,4k内存,2k显存,16个寄存器(其中15个通用 ...
- POJ 3321 Apple Tree dfs+二叉索引树
题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
- Mvc学习笔记(2)
Razor模板的具体语法使用 因为Razor模板的可以自动识别<>,大大减少了代码量,本节我们一起来探究Razor模板的语法的简单应用: MVC知识点: 1.ASP.NET Mvc框架 是 ...