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,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...
随机推荐
- 转:ASP.NET前台代码绑定后台变量方法总结
经常会碰到在前台代码中要使用(或绑定)后台代码中变量值的问题.一般有<%= str%>和<%# str %>两种方式,这里简单总结一下.如有错误或异议之处,敬请各位指教. 一方 ...
- gnome-shell 使用 notify-send 发送桌面消息
什么是notify-send? notify-send - a program to send desktop notifications 怎么使用? NAME notify-send - a pro ...
- SELinux 关闭方法
临时关闭selinux,可执行: setenforce 0 // 设置selinux状态 1启用,0警告.不启用. getenforce //检查selinux状态是否生效 永久关闭selinu ...
- python基础学习15----异常处理
异常处理,是编程语言或计算机硬件里的一种机制,用于处理软件或信息系统中出现的异常状况(即超出程序正常执行流程的某些特殊条件). 1.异常的类型 异常的类型多种多样,常见的异常有: AttributeE ...
- Shell学习---Shell脚本的静态检查工具shellcheck
Shell脚本的静态检查工具shellcheck ubuntu下 apt install shellcheck ,即可安装shellcheck.写完shell脚本,记得用它检查一下,能给你点建议的.要 ...
- selenium-百度搜索框输入后,定位联想下拉框元素
1.输入关键字后,显示联想下拉框,鼠标右键对应的联想字段,点击检查,就可在F12模式下元素查看器中定位到,之后使用Xpath定位.
- Linux运维之系统性能瓶颈工具vmstat分析
vmstat是一个很好用的检测系统性能工具,没有过多的参数,直接一个vmstat命令即可,不过我们一般加上-w表示宽格式输出.然后再附加上侦测时间即可 例如: vmstat 表示每3秒检测一次并输出系 ...
- Docker容器学习与分享02
1.docker容器的创建 首先运行一个centos容器,感受一下Docker容器的便捷 首先先看一下镜像仓库 发现仓库里没有镜像,也就是没有创建容器的模板,这时考虑从REPOSITORY中拉取镜像( ...
- 面向对象程序设计__Task6_Calculator1.6.2
The 4th part of the Calculator program _ Interface 题目链接:第六次作业(计算器第四步) github链接:Calculator_1.6.2 第六次作 ...
- 【2017下集美大学软工1412班_助教博客】团队作业3——需求改进&系统设计团队成绩公示
作业要求 团队作业3--需求改进&系统设计 团队评分结果 Total QH BZ GJ MS SXX WBS SJ JG SJK JH NR CS PHILOSOPHER 需求改进&系 ...