【一天一道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 ...
随机推荐
- linux中probe函数传递参数的寻找(下)
点击打开链接 linux中probe函数传递参数的寻找(下) 通过追寻driver的脚步,我们有了努力的方向:只有找到spi_bus_type的填充device即可,下面该从device去打通,当两个 ...
- MPAndroidChart——饼图
MPAndroidChart--饼图 MPAndroidChart是安卓下的一个开源图形库,很多效果,简单看几个效果图 Github地址:https://github.com/PhilJay/MPAn ...
- 1CCTableView的使用,TableView响应和小格子tableView实现
1 CCTableView的使用 T26TableView.h #ifndef __T26TableView_H__ #define __T26TableView_H__ #includ ...
- iOS下JS与OC互相调用(四)--JavaScriptCore
前面讲完拦截URL的方式实现JS与OC互相调用,终于到JavaScriptCore了.它是从iOS7开始加入的,用 Objective-C 把 WebKit 的 JavaScript 引擎封装了一下, ...
- 4.0、Android Studio配置你的构建
Android构建系统编译你的app资源和源码并且打包到APK中,你可以用来测试,部署,签名和发布.Android Studio使用Gradle,一个高级的构建套件,来自动化和管理构建进程,同时可以允 ...
- UNIX网络编程——Socket粘包问题
一.两个简单概念长连接与短连接:1.长连接 Client方与Server方先建立通讯连接,连接建立后不断开, 然后再进行报文发送和接收. 2.短连接 Client方与Server每进行一次报文收发交易 ...
- [EXtJS5学习笔记]第一节 Sencha Cmd 学习笔记 简介 Sencha Cmd是什么
本文地址: http://blog.csdn.net/sushengmiyan/article/details/38295575 本文作者:sushengmiyan ----------------- ...
- Win 10 下 android studio显示 Intel haxm无法安装,以及VT-X和hyper-x的冲突问题
我 的电脑是神舟战神k650c i7 D4,处理器是Intel core i7 4710-MQ,系统是win 10的 我心血来潮想学习一下安卓开发,就首先安装了android s ...
- SDL2源代码分析3:渲染器(SDL_Renderer)
===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDL_Init()) SDL ...
- 见过的最全的iOS面试题
之前看了很多面试题,感觉要不是不够就是过于冗余,于是我将网上的一些面试题进行了删减和重排,现在分享给大家.(题目来源于网络,侵删) 1. Object-c的类可以多重继承么?可以实现多个接口么?Cat ...