思路:
  • 暴力,复杂度为 \(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. Python数据处理——numpy_3

    通过前面两次的学习,基本上对numpy有了一定的认识,所以,接下来进一步对numpy学习.同时,最后以一个有趣的例子加深对numpy的理解. import numpy as np xarr = np. ...

  2. Laravel 5.2 教程 - 数据填充

    一.简介 Laravel提供的填充类(seed),可以让大家很容易的实现填充测试数据到数据库.所有的填充类都位于database/seeds目录.填充类的类名完全由你自定义,但最好还是遵循一定的规则, ...

  3. input元素之间的融合

    将两个input融合在一起,注意input标签之间的空格 .put1{ width: 20px; height: 28px; vertical-align:middle; border: 1px so ...

  4. android webview和 javascript 进行交互

    HTML5进行app开发具有开发快,跨平台等优点,但是当客户需要访问照相机或者调用摄像头等硬件的时候,H5就会有限制,必须要调用原生方法进行设备访问.下面简要介绍JS和原生方法互相调用的方法: 1 在 ...

  5. YAHOO 34 条前端优化建议

    雅虎团队经验:网站页面性能优化的34条黄金守则 1.尽量减少HTTP请求次数       终端用户响应的时间中,有80%用于下载各项内容.这部分时间包括下载页面中的图像.样式表.脚本.Flash等.通 ...

  6. oracle一直不确定的distinct多字段处理情况整理

    第一步,建一个表,表数据如下: 第二步:发现叫豆豆的是两只狗,一只是金毛犬,一只狼青. 如果我用 select 宠物名称,宠物大类 from test_1;返回结果就只有一条. 如果我用 select ...

  7. JavaScript中的DOM函数与关键字汇总

    DOM节点的属性 属性 描述 attributes数组 获取某个节点的所有属性子节点(实际是一个NodeList对象) childNodes数组    获取某个节点的所有子节点,可以按数组方式访问子节 ...

  8. 【JAVAWEB学习笔记】12_Http&Tomcat

    一.Http协议 1.什么是Http协议 HTTP,超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的     一种网络协议.所有的WWW文件都必须遵守这 ...

  9. xcode实用快捷键

    command + R 快速编译并运行项目 command + . 停止正在运行的项目 command + shift + O 快速打开xcode文件搜索功能 command + 0 关闭左边的侧边栏 ...

  10. Spring Cloud 客服端负载均衡 Ribbon

    一.简介   Spring Cloud Ribbon 是一个基于Http和TCP的客服端负载均衡工具,它是基于Netflix Ribbon实现的.它不像服务注册中心.配置中心.API网关那样独立部署, ...