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,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...
随机推荐
- 在 Azure 中的 Windows 虚拟机上使用 SSL 证书保护 IIS Web 服务器
若要保护 Web 服务器,可以使用安全套接字层 (SSL) 证书来加密 Web 流量. 这些 SSL 证书可存储在 Azure Key Vault 中,并可安全部署到 Azure 中的 Windows ...
- 大话存储 3 - 七种磁盘RAID技术
RAID技术 Redundant Array of Independent Disks 由独立的磁盘组成的具有冗余特性的阵列. 有两个特性: 阵列:需要很多磁盘来组成 冗余:允许某块磁盘损坏之后,数据 ...
- 微信小程序里使用阿里巴巴矢量图标
登录 阿里巴巴矢量图标 (https://www.iconfont.cn) 选中图标,加入购物车图标 下载源代码 解析出来如下文件结构 有两种使用方式: 1)不转换成base64的文件 找到 icon ...
- 01-Angularjs开发环境搭建
一.概述## 时下web前端非常热门,新的框架层出不穷,web前端的三驾马车VueJS.AngularJS.ReactJS发展非常迅猛.其中VueJS是国人开发的项目,百度热点趋势以1000%的速度在 ...
- C#实现的协同过滤算法
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace SlopeOn ...
- 各种SQL查询技巧汇总 (转)
原文地址: https://blog.csdn.net/tim_phper/article/details/54963828 select select * from student; all 查询所 ...
- 主机ping不通virtualbox虚拟机的解决办法
虚拟机与主机之间相互ping通有一个问题,就是虚拟机能够ping通主机 本地主机ping不通虚拟机: 解决办法: 1)如果虚拟机有两个网卡: 将虚拟机网卡2的连接方式改成桥接即可: ⚠️要将虚拟机重启 ...
- Android 文件的读取和写入
(1)openFileInput和openFileOutput的使用 文件的使用,注意最后要用finally给关闭掉. openFileOutput:(写入文件,如果没有文件名可以创建,这里不需要判断 ...
- WorldWind源码剖析系列:下载队列类DownloadQueue
下载队列类DownloadQueue代表具有优先级的下载队列,该类的存储下载请求的数组链表专门按一定的优先级来存储下载请求的.该类的类图如下. 下载队列类DownloadQueue各个字段的含义说明如 ...
- OpenCV——KAZE、AKAZE特征检测、匹配与对象查找
AKAZE是KAZE的加速版 特征点查找和绘制:把surf中的surf改成KAZE或AKAZE即可 #include <opencv2/opencv.hpp> #include < ...