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,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...
随机推荐
- Safari中的Date
在js中处理Date时,发现Safari和其他浏览器的支持方式不一致 1.例如:2017-01-01 12:00:00 在其他浏览器中,使用这个格式的字符串进行new Date操作没有问题,但是在Sa ...
- Chrome 无法加载Shockwave Flash
遇到的问题 Chrome经常出现上图的提示,把Adobe Flash重装了N多次也是无法解决此问题,经多次尝试终于解决此问题. 解决方法 1.在Chrome地址栏输入:chrome://plugins ...
- python基础学习1
一.python第一个程序 print("hello world") 二.变量的命名规则 1. 字母数字下划线组成 2. 不能以数字开头,不能含有特殊字符和空格 3. 不能以保留字 ...
- ZooKeeper 分布式协调服务介绍
0. 说明 从自己的独立博客迁移,该部分为 Zookeeper分布式协调服务介绍 原文链接 ZooKeeper 指南 1. ZooKeeper 简介 [官方介绍] ZooKeeper 是一种集中式服 ...
- 探索C#字符串
一.前言 刚接触C#时,书上说string是一种特殊的引用类型,因此string类型变量在作为参数传递到另一个方法,被修改后原变量的值不会发生变化,当时看得我一脸懵逼,什么叫特殊....后来又听说字符 ...
- 极限编程核心价值:勇气(Courage)
原文:https://deviq.com/courage 极限编程核心价值:简单(Simplicity) 极限编程核心价值:沟通(Communication) 极限编程核心价值:反馈(Feedback ...
- 9.Solr4.10.3数据导入(post.jar方式和curl方式)
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.使用post.jar方式 java -Durl=http://192.168.137.168:8080/s ...
- python第二十七课——os模块
演示os模块中常用的属性和函数: 1.os模块: 作用:管理文件和目录 属性: os.name:返回系统类型 常用的windows系统 --> nt os.environ:返回当前系统所有的环境 ...
- Spring之强制修改某个方法的行为(Arbitrary method replacement)
A less commonly useful form of method injection than Lookup Method Injection is the ability to rep ...
- Spring Boot Actuator RCE
来看一下IDEA如何调试Spring Boot 先在https://github.com/artsploit/actuator-testbed下载源码 如下命令就能通过maven环境启动 mvn in ...