16.3Sum Closet
思路:
- 暴力,复杂度为 \(O(n^3)\),超时
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int res = 10000;
int tmp = 0;
int len = nums.size();
for(int i = 0; i < len; i++){
for(int j = i+1; j < len; j++){
for(int k = j+1; k < len; k++){
if(res > abs(target - nums[i] - nums[j] - nums[k])){
res = abs(target-nums[i] - nums[j] - nums[k]);
tmp = nums[i] + nums[j] + nums[k];
}
}
}
}
return tmp;
}
};
- 类似3Sum。这几道题相对暴力能够减小复杂度的主要原因是暴力里面的两层循环都执行了,而后面的这种解法通过比较使内部的两层循环减少到了一层,所以时间复杂度由 \(O(n^3)\) 减小到了 \(O(n^2)\)。
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int res = 100000;
int tmp = 0;
sort(nums.begin(), nums.end());
int len = nums.size();
for(int i = 0; i < len-2; i++){
int lo = i+1,hi = len-1;
while(lo < hi){
if(abs(nums[i] + nums[lo] + nums[hi] - target) < res){
res = abs(nums[i] + nums[lo] + nums[hi] - target);
tmp = nums[i] + nums[lo] + nums[hi];
}
if(nums[i] + nums[lo] + nums[hi] == target) return target;
else if (nums[i] + nums[lo] + nums[hi] > target){
hi--;
}
else lo++;
}
}
return tmp;
}
};
16.3Sum Closet的更多相关文章
- 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][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, ...
- [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 ...
随机推荐
- 如何快速理解JavaScript 中重要语句for循环
一.基本结构:for(起始状态:判断条件:状态改变){ 执行语句: } 执行顺序:for(var i=1;i<3;i++){ alert(i); } 1.判断条件 2.执行语句 3. ...
- Dubbo的使用简介
Dubbo是什么 官方定义 DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000, ...
- 速成制作rpm包
FPM 由于很多软件在安装时需要编译,这会浪费不少的时间,为了提升部署效率,于是就想到制作rpm包.通常rpm包的制作是使用rpmbuild命令来制作,但是你需要知道它的语法,比较繁琐.这就用到了FP ...
- ArrayList 遍历
1.迭代器遍历 package sourceCode.ArrayList; import java.util.ArrayList; import java.util.Iterator; import ...
- LeetCode 207. Course Schedule(拓扑排序)
题目 There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have p ...
- centos GUI界面与命令行的切换
Linux 系统任何时候都运行在一个指定的运行级上,并且不同的运行级的程序和服务都不同,所要完成的工作和所要达到的目的都不同.Centos设置了如下表所示的运行级,并且系统可以在这些运行级别之间进行切 ...
- Display:table;妙用,使得左右元素高度相同
我们在设计网页的时候,为了左右能够分明一点,我们经常会在左边元素弄一个border-right,但是出现一个问题,如果左边高度比较小,这根线就短了,下面空了一部分,反正如果在右边的元素弄一个borde ...
- hdu1054最小顶点覆盖
最小定点覆盖是指这样一种情况: 图G的顶点覆盖是一个顶点集合V,使得G中的每一条边都接触V中的至少一个顶点.我们称集合V覆盖了G的边.最小顶点覆盖是用最少的顶点来覆盖所有的边.顶点覆盖数是最小顶点覆盖 ...
- poj3304计算几何直线与线段关系
Given n segments in the two dimensional space, write a program, which determines if there exists a l ...
- C#之lambda表达式
从C#3.0开始,可以使用lambda表达式把实现代码赋予委托.lambda表达式与委托(http://www.cnblogs.com/afei-24/p/6762442.html)直接相关.当参数是 ...