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 number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
给定一个数组及一个目标值,找出三个数,使它们的和与目标值的差值最小,输出它们的和。
解法
与上一题3Sum一样,遍历第一个数,然后用双指针算法遍历剩下两个数,随时比较它们与目标值的差值。
class Solution
{
public:
int threeSumClosest(vector<int>& nums, int target)
{
sort(nums.begin(),nums.end());
int diff = 0x7fffffff;
int ans = 0;
for(int i = 0;i < nums.size();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) < diff)
{
diff = abs(sum - target);
ans = sum;
}
if(sum < target)
j ++;
else
k --;
}
}
return ans;
}
};
LeetCode 3Sum Closest (Two pointers)的更多相关文章
- [LeetCode]3Sum Closest题解
3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...
- [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 ...
- 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 ...
- LeetCode——3Sum Closest
Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- leetcode 3Sum Closest python
class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List ...
- LeetCode 3Sum Closest 最近似的3sum(2sum方法)
题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...
- 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 ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- 3Sum Closest - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...
随机推荐
- iis 发布asp.net mvc 网站时候js css 压缩问题,图片不加载问题
一.JS CSS 自动压缩问题 默认情况下mvc这个框架会把css,js文件压缩成一个js或者css文件,一会发现只有一个<link href="/Content/css?v=ji3n ...
- EntityFramework 贪婪加载与延迟加载以及资源回收
EntityFramework的资源回收 1) Using 内包含Entity的上下文关系,对俩表做Add操作,最好可以直接写一个 entity.SaveChanges(); 完成两张表的同时add操 ...
- sysbench 多线程异步io模拟mysql测试的脚本
用于测试的脚本: for size in 100 do cd /mnt/stec sysbench --test=fileio --file-num=1 --file-total-size=${siz ...
- PAT 1025 反转链表
PAT (Basic Level) Practise 1025 Github链接:https://github.com/H-BING/object-oriented/tree/master/PAT10 ...
- SDN2017 第二次实验作业
安装floodlight 参考链接:http://www.sdnlab.com/19189.html 从github下载源码,并编译安装 $ sudo apt-get install build-es ...
- objc.io 待看文章
https://objccn.io/issues/ https://objccn.io/issues/ 使用 VIPER 构建 iOS 应用 并发编程
- 乐视4.14硬件免费日de用户体验
此贴用于记录2016年4月14日乐视硬件免费日购买X65超级电视的用户体验.后续将动态更新 我是乐视电视的第一批用户,从乐视上市第一批超级电视,我先后帮助家人.同事.朋友买了6台乐视超级电视,也算是乐 ...
- Spark项目之电商用户行为分析大数据平台之(十二)Spark上下文构建及模拟数据生成
一.模拟生成数据 package com.bw.test; import java.util.ArrayList; import java.util.Arrays; import java.util. ...
- 服务发现系统etcd之安装和使用
一.概述 etcd是一个高可用的键值存储系统,主要用于共享配置和服务发现.etcd是由CoreOS开发并维护的,灵感来自于 ZooKeeper 和 Doozer,它使用Go语言编写,并通过Raft一致 ...
- persistence_timeout ,域名请求登录后一操作即被踢出,,KeepAlive,lvs
virtual_server *.*.*.* 80 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 120 protocol TCP ...