【一天一道LeetCode】#16. 3Sum Closest
一天一道LeetCode系列
(一)题目:
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).
(二)解题
直接用三重循环,然后考虑到重复的数字,则需要先排序,以便于后续去重。
其次,当等于target时,则直接返回
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int min = 2147438647;
int key =0;
std::sort(nums.begin() , nums.end());
for(int i = 0 ; i < nums.size()-2 ; )
{
for(int j = i+1 ; j < nums.size()-1 ;)
{
for(int k = j+1 ; k < nums.size() ; )
{
int gap = nums[i]+nums[j]+nums[k];
int temp = gap-target>0?gap-target:target-gap;
if(temp<min){
min = temp;
key = gap;
if(min ==0) //如果找到等于0的则返回
{
return key;
}
}
k++;
while(k<nums.size() && nums[k] == nums[k-1]) ++k;
}
j++;
while(j<nums.size()-1 && nums[j] == nums[j-1]) ++j;
}
i++;
while(i<nums.size()-2 && nums[i] == nums[i-1]) ++i;
}
return key;
}
};
在网上看到另外一种快速的解法。O(n^2)
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
std::sort(nums.begin() , nums.end());
bool isfirst = true;
int ret;
for(int i = 0 ; i < nums.size() ; i++)
{
int j = i+1;
int k = nums.size()-1;
while(j<k){
int sum = nums[i]+nums[j]+nums[k];
if(isfirst)
{
ret = sum;
isfirst = false;
}
else
{
if(abs(sum - target) < abs(ret - target))
{
ret = sum;
}
}
if(ret == target)
return ret;
if(sum>target)
k--;
else
j++;
}
}
return ret;
}
};
【一天一道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, ...
- [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 ...
- 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 解题思路
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 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——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...
- 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- [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 ...
随机推荐
- EBS开发技术之trace
trace的目的 trace主要是用于程序调优,优化,程序bug调试,程序运行系统情况跟踪 trace步骤 1.并发定义中,勾上"启用跟踪" 2.提交一个请求,得到请求编号 注意: ...
- android推荐使用dialogFrament而不是alertDialog
DialogFragment在android 3.0时被引入.是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框.典型的用于:展示警告框,输入框,确认框等等. 在Dia ...
- sql中InnoDB和MyISAM的区别
InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型 1,MyISAM类型的表强调的是性能,其执行数度比InnoDB类型更快,但是不提供事务支持等高级处理,往往被认为只适合小项目:而 ...
- 调用MediaScannerConnection 发生内存泄露的解决方法
调用MediaScannerConnection发起扫描时经常会发生内存泄露,例如: E ActivityThread: Activity FolderListActivity has leaked ...
- Mybatis代码自动生成插件使用
1.配置pom.xml 添加mybatis-generator-maven-plugin到pom.xml. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...
- 卸载linux后出现grub rescue怎么办?
原文转自:http://zhidao.baidu.com/link?url=9e2mOttgV0IJDMml58SFbV-7XOvVzp2jR5l1n3ltFOzX1XAcp5-t-QQPc-Nozy ...
- 精通CSS+DIV网页样式与布局--制作实用菜单
在上篇博文中,小编中主要的简单总结了一下CSS中关于如何设置页面和浏览器元素,今天小编继续将来介绍CSS的相关基础知识,这篇博文,小编主要简单的总结一下在CSS中如何制作网页中的菜单,这部分的内容包括 ...
- OLAP工作的基本概念(结合个人工作)
OLTP和OLAP 传统的数据库系统都是OLTP,只能提供数据原始的操作.不支持分析工作. OLTP系统::执行联机事务和查询处理.一般超市进销存系统,功能:注册,记账,库存和销售记录等等, OLAP ...
- hive的map类型处理
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-CollectionFunc ...
- BIP Requests Are Failing With Error "OPP Error Oracle.apps.xdo.XDOException: Error Creating Lock Fil
In this Document Symptoms Cause Solution References Applies to: BI Publisher (formerly XML P ...