思路:
  • 暴力,复杂度为 \(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. 【转】JDBC学习笔记(8)——数据库连接池(dbcp&C3P0)

    转自:http://www.cnblogs.com/ysw-go/ JDBC数据库连接池的必要性 一.在使用开发基于数据库的web程序时,传统的模式基本是按一下步骤: 1)在主程序(如servlet/ ...

  2. C语言学习第四章

    今天学习C语言循环结构,为什么要用循环呢?因为有时候我们对一堆的数字进行重复的处理的时候要重复的编写一些相同或者差不多的代码,让程序显得很臃肿,而且写着也麻烦,如果用循环来写的话能简化很多,出错的话也 ...

  3. bootstrap快速入门笔记(一)

    一,头部基本格式:<head lang="en"> <meta charset="UTF-8"> <meta name=" ...

  4. 安卓和iOS移动APP开发设计应该考虑哪些问题

    应该考虑哪些问题 现在用移动设备上网的用户越来越多,企业也可以从移动APP端挖掘潜在用户,想要设计好一款移动APP,我们需要考虑哪几个问题呢? 首先 我们应该先确认清楚企业的移动互联网战略目标是什么? ...

  5. Linux Shell——函数的使用

    文/一介书生,一枚码农. scripts are for lazy people. 函数是存在内存里的一组代码的命名的元素.函数创建于脚本运行环境之中,并且可以执行. 函数的语法结构为: functi ...

  6. Struts2拦截器登录验证

    Struts2拦截器 Struts2拦截器的概念和Spring Mvc拦截器一样. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截 ...

  7. Python 基础之 异常处理

    python 基础之异常处理 说到异常处理,就得先问一下,什么是异常处理?  先来看一下,什么是异常? 异常就是:程序运行时发出的错误的信号. 异常的种类先来看一下: 一.常见的异常 Attribut ...

  8. 从Python小白到第一个小游戏发布

    1.安装必要的环境(附图两张) 直接下载安装程序,本人win10系统,根据电脑系统下载并安装对应的python.exe,安装路径可以选择D盘的,具体安装细节这里就不说了,不知道的可以留言或者找度娘 2 ...

  9. opencv探索之路(一):win10 X64+VS2015+opencv3.10安装教程

    我的电脑64位Win10系统,现在利用VS2015安装opencv3.10.安装之路颇为艰辛,从一开始的VS2015的安装,到opencv的安装,都充满挑战,历经千辛万苦终于把opencv开发环境搭建 ...

  10. Mysql求百分比

    根据相应条件抽出相应count数(myCount) 抽出总count数(totalCount) 计算百分比:myCount / totalCount * 100 四舍五入:使用ROUND函数ROUND ...