LeetCode(16)题解--3Sum Closest
https://leetcode.com/problems/3sum-closest/
题目:
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).
思路:
和上一道差不多,主要还是有序数组里两个flag移动。
AC代码:
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int res,t,j,k,lowerbound=INT_MAX,n=nums.size();
sort(nums.begin(),nums.end());
for(int i=;i<n-;i++){
t=target-nums[i];
j=i+;
k=n-;
while(j<k){
if(abs(nums[j]+nums[k]-t)<lowerbound){
res=nums[i]+nums[j]+nums[k];
lowerbound=abs(nums[j]+nums[k]-t);
}
if(nums[j]+nums[k]<t)
j++;
else
k--;
}
}
return res;
}
};
LeetCode(16)题解--3Sum Closest的更多相关文章
- LeetCode(15)题解--3Sum
https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c i ...
- LeetCode(16)3Sum Closest
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- leetcode第16题--3Sum Closest
Problem:Given an array S of n integers, find three integers in S such that the sum is closest to a g ...
- LeetCode题解——3Sum Closest
题目: 给定一个数组,和一个指定的值,找出数组中3个数,它的和最接近这个指定值,并返回这个和. 解法: 和上一题找3个数的和为0一样,先排序再遍历,这一次不需要记录路径. 代码: class Solu ...
- LeetCode OJ: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】016 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- leetcode笔记:3Sum Closest
一.题目描写叙述 二.解题技巧 该题与3Sum的要求类似.不同的是要求选出的组合的和与目标值target最接近而不一定相等.但实际上,与3Sum的算法流程思路类似,先是进行排序.然后顺序选择数组A中的 ...
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
随机推荐
- 洛谷P1101 单词方阵
题目描述 给一nXn的字母方阵,内可能蕴含多个“yizhong”单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着8个方向的任一方向,同一单词摆放时不再改变方向,单词与单词之间[color=red ...
- net5:动态修改内存中的站点地图节点
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- http client transfer
背景 在平时工作中我偶尔会写一些脚本监控HTTP接口的健康状况,基本上都是发送HTTP GET或HTTP POST请求,然后检测响应内容.但一直用的是WebClient和HttpWebRequest, ...
- 解决npm 的 shasum check failed for错误
使用npm安装一些包失败,类似如下报错情况: C:\Program Files\nodejs>npm update npm npm ERR! Windows_NT 10.0.14393 np ...
- Scrollview总结:滑动问题、监听Scrollview实现头部局改变
ScrollView就是一个可以滚动的View,这个滚动的方向是垂直方向的,而HorizontalScrollView则是一个水平方向的可以滚动的View. ScrollView的简单介绍 Scrol ...
- js-异步请求音频完成后页面显示
var ajax = new XMLHttpRequest(); ajax.open("get", "http://gzmylike.wedei.com/zt/gzyan ...
- Codeforces Gym 100203H Highways 最小生成树
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 给你平面上若干点,生成一颗完全图,让 ...
- Codeforces Gym - 101147G The Galactic Olympics
Discription Altanie is a very large and strange country in Mars. People of Mars ages a lot. Some of ...
- Ant -----ant标签和自定义任务
随便记一下 Ant的用法吧.ant ,maven, gradle ,三个打包工具到齐了,Ant 常见标签解析,ant 自定义task . <?xml version="1.0" ...
- Maven修改默认中央仓库
其实Maven的默认仓库是可以修改的.比如使用阿里云的镜像地址等. 修改步骤: 1.打开{M2_HOME}/conf/settings.xml文件,找到mirrors节点,修改如下代码: <mi ...