思路:
  • 暴力,复杂度为 \(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的更多相关文章

  1. 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 = ...

  2. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  3. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  4. 《LeetBook》leetcode题解(16):3Sum Closest [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. 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 ...

  6. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  7. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  8. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  9. 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 ...

随机推荐

  1. 0-创建scott示例数据

    CREATE TABLE dept (  deptno INT PRIMARY KEY,  dname VARCHAR(14),  loc VARCHAR(13) );   INSERT INTO d ...

  2. 《分布式Java应用之基础与实践》读书笔记一

    分布式Java应用的体系结构知识简单分为: 网络通信:包括协议和IO 消息方式的系统间通信:包括基于Java包.基于开源框架.性能角度 远程调用方式的系统间通信:包括基于Java包.基于开源框架.性能 ...

  3. Asp .net core api+Entity Framework 实现数据的存取到数据库中

    最近在学dotNetCore 所以尝试了一下api 这个功能 不多说了大致实现如下 1.用vs2017建立一个Asp.net  Core Web 应用程序 在弹出的对话框中选择 Web API 项目名 ...

  4. oracle 的 SDO_GEOMETRY

    元数据定义 CREATE OR REPLACE TYPE MDSYS.SDO_GEOMETRY AS OBJECT ( SDO_GTYPE NUMBER, SDO_SRID NUMBER, SDO_P ...

  5. JSP动态员工登陆案例

    package web; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import j ...

  6. 关于Yii框架的基础知识

    第一次写博文,也不知道怎么写,不太熟悉,带小伙伴学习一样我日常使用的Yii框架. PHP中的开发框架有很多,比如:ThinkPHP.Yii.CI.Laravel.Phalcon等.现在流行度最高的是L ...

  7. 对游览器遭到劫持的处理方案(RemoveAds Not By This Site)

    近期游览器一直被劫持 :火狐用不了,ie出毛病了,谷歌游览器也不好使了,已经快被逼疯!!! 但是事情总是向着美好的方向发展的么! 接下来就和大家一起分享一下我的解决经验! 首先用360杀毒(虽然大多数 ...

  8. linux-redhat-iso 下载

    http://archive.download.redhat.com/pub/redhat/linux/9/en/iso/i386/ http://www.jb51.net/do/plus/downl ...

  9. C#集合之并发集合

    .NET 4 开始,在System.Collection.Concurrent中提供了几个线程安全的集合类.线程安全的集合可防止多个线程以相互冲突的方式访问集合. 为了对集合进行线程安全的访问,定义了 ...

  10. Gulp文档入门的文档

    Gulp自动化执行文件的操作 首先gulp基于node开发的,先按照node.js,使用npm sudo npm install -g gulp (在全局的范围安装 gulp) gulp --help ...