Question

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

For example, given array S = {-1 2 1 -4}, and target = 1.

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

Solution

  • 先排序,然后遍历每一个元素nums[i],并且在其之后找nums[j],nums[k],j<k,使得结果最接近。
sum = nums[i] + nums[j] + nums[k];

if sum > target k--;

if sum < target j++;

if sum == target return;

Code

class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int minValue = nums[0] + nums[1] + nums[2];
int res = abs(minValue - target);
for (int i = 0; i < nums.size() - 2; i++) {
int j = i + 1;
int k = nums.size() - 1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
if (abs(sum - target) < res) {
res = abs(sum - target);
minValue = sum;
}
if (sum > target)
k--;
else if (sum < target)
j++;
else
return sum;
}
}
return minValue;
}
};

LeetCode——3Sum Closest的更多相关文章

  1. [LeetCode]3Sum Closest题解

    3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...

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

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  3. Leetcode 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  4. LeetCode 3Sum Closest (Two pointers)

    题意 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  5. leetcode 3Sum Closest python

    class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List ...

  6. LeetCode 3Sum Closest 最近似的3sum(2sum方法)

    题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...

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

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

  8. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

  9. 3Sum Closest - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...

随机推荐

  1. How TCP clients and servers communicate using the TCP sockets interface

    wTCP客户端和服务器是如何通过TCP套接字接口进行通讯的.服务器距离.负载,网络拥堵. HTTP The Definitive Guide We begin with the web server ...

  2. python open-falcon docker.WEB developers---flask,---django.

    http://www.verydemo.com/demo_c281_i2477.html (python Gevent – 高性能的Python并发框架) http://www.django-rest ...

  3. ReferenceQueue

    @Test public void strongReference() { Object referent = new Object(); /** * 通过赋值创建 StrongReference * ...

  4. mybatis拓展框架对比

    对比项 mybatis-plus mybatis-enhance mapper fastmybatis 表名映射 @TableName @Table JPA:@Table JPA:@Table 主键映 ...

  5. python学习笔记(三)函数

    一.定义函数 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可. 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参 ...

  6. django 使用form验证用户名和密码

    form验证可以减少查询数据库,所以代码先预先验证,有问题可以返回给前端显示 1.在users文件夹下新建forms.py文件,用来验证用户名和密码是否为空,密码长度是否大于6 # -*- codin ...

  7. MFC中存在的不属于任何类的全局函数,它们统统在函数名称开头加上Afx

    MFC中存在的不属于任何类的全局函数,它们统统在函数名称开头加上Afx. 函数名称 说明 AfxWinInit 被WinMain(MFC提供)调用的一个函数,用做MFC GUI程序初始化的一部分,如果 ...

  8. JQuery可以轻松实现数字框

    如果使用JQuery,则可以轻松实现数字框. <input type="text" name="name" value="0" run ...

  9. iis 反向代理 组件 Application Request Route

    安装后要重启服务器. 不然 IIS 不会生效.

  10. kubernetes rabbitmq 集群安装配置

    生成secret来保存relang-cookie: secret.yml文件: apiVersion: v1kind: Secretmetadata: name: rabbit-cookietype: ...