leetcode-algorithms-16 3Sum Closest

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

解法

解法同题15

class Solution
{
public:
int threeSumClosest(vector<int>& nums, int target)
{
// If less then 3 elements then return their sum
while (nums.size() <= 3)
{
return std::accumulate(nums.begin(), nums.end(), 0);
} std::vector<int> v(nums.begin(), nums.end());
std::sort(v.begin(), v.end()); int sum = 0;
int n = v.size();
int ans = v[0] + v[1] + v[2];
for (int i = 0; i < n-2; i++)
{
int j = i + 1;
int k = n - 1;
while (j < k)
{
sum = v[i] + v[j] + v[k];
if (abs(target - ans) > abs(target - sum))
{
ans = sum;
if (ans == target)
return ans;
}
(sum > target) ? k-- : j++;
}
} return ans;
}
};

空间复杂度: O(n^2).

时间复杂度: O(1)

链接: leetcode-algorithms 目录

leetcode-algorithms-16 3Sum Closest的更多相关文章

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

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

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

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

  3. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  4. 【一天一道LeetCode】#16. 3Sum Closest

    一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...

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

  6. 【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 ...

  7. LeetCode:16. 3Sum Closest(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...

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

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

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

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

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

随机推荐

  1. (转载)MySQL基础(非常全)

    MySQL基础 一.MySQL概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access ...

  2. myCat知识笔记

    数据字典做成全局表(在各个分库中都有备份) ER表, 关联表都放在同一个分库上, 有利于数据关联查询 一致性hash ,/hash slot 主要为了解决分布式节点扩容时, 迁移数据的问题. mySq ...

  3. 【JS】JavaScript中innerHTML与innerText,createTextNode的区别

    innerHTML和innerText 它们都会把元素内内容替换掉,区别在于: innerHTML 会把替换内容里的 HTML 标记解释执行. innerText 会把替换内容里的 HTML 标记原样 ...

  4. 4、lvs nat和dr类型演示

    实战操作 LVS-NAT  (应用场景:VIP是公网地址,DIP和RIP一般使用私网地址,NAT的主要目的是为了隐藏服务器) 核心要点: 1.DIP与各real server的RIP必须在同一个网段中 ...

  5. Lintcode521-Remove Duplicate Numbers in Array-Easy

    Description Given an array of integers, remove the duplicate numbers in it. You should: Do it in pla ...

  6. 51nod 1378 夹克老爷的愤怒(树型dp+贪心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1378 题意: 思路:要想放得少,尽量放在叶子节点处,叶子节点处点比较多. ...

  7. 【Python】【数据库】

    #[[数据库]]'''MySQL是Web世界中使用最广泛的数据库服务器.SQLite的特点是轻量级.可嵌入,但不能承受高并发访问,适合桌面和移动应用.而MySQL是为服务器端设计的数据库,能承受高并发 ...

  8. vue--移动端兼容问题

    click的300ms延迟: 引入fastclick库来解决 输入命令 npm install fastclick 在main.js导入 import Vue from 'vue' import Ap ...

  9. ajax 怎么重新加载页面

    $.ajax({ type:"post", url:url, data:{xxx:xx}, dataType: "json", success : functi ...

  10. Python 常见时间处理

    Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时间间隔是以秒为单位的浮点小数. ...