leetcode133:3sum-closest
题目描述
例如,给定的整数 S = {-1 2 1 -4}, 目标值 = 1.↵↵ 最接近目标值的和为 2. (-1 + 2 + 1 = 2).
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).
输出
0
链接:https://www.nowcoder.com/questionTerminal/291a866d7c904563876b373b0b157dde?f=discussion
来源:牛客网
class Solution {public: int threeSumClosest(vector<int> &num, int target) { int n = num.size(); int result = num[0] + num[1] + num[n-1]; sort(num.begin(),num.end()); for(int i=0;i<n-2;i++) { int start = i + 1; int end = n - 1; while(start < end) { int sum = num[i] + num[start] + num[end]; if(sum < target) start++; else end--; if(abs(sum-target) < abs(result-target)) result = sum; } } return result; }};leetcode133:3sum-closest的更多相关文章
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 2Sum,3Sum,4Sum,kSum,3Sum Closest系列
1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- 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 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- LeetCode--No.016 3Sum Closest
16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...
- leetcode-algorithms-16 3Sum Closest
leetcode-algorithms-16 3Sum Closest Given an array nums of n integers and an integer target, find th ...
随机推荐
- CF877E Danil and a Part-time Job
题目大意: link 有一棵 n 个点的树,根结点为 1 号点,每个点的权值都是 1 或 0 共有 m 次操作,操作分为两种 get 询问一个点 x 的子树里有多少个 1 pow 将一个点 x 的子树 ...
- Visual C# 制作DLL文件
一.制作.dll1.首先创建一个新类库工程文件 文件->新建->项目->Visual C#->类库.填入工程文件名称,并且选择文件要存放的目录. 2.工程文件 将Class1 ...
- C++调用全局函数与类成员函数
void testfunc(void *param) { printf("\n\tcall global function %s\n", param); } void *GetCl ...
- Redis 客户端 Jedis、lettuce 和 Redisson 对比
Redis 支持多种语言的客户端,下面列举了部分 Redis 支持的客户端语言,大家可以通过官网查看 Redis 支持的客户端详情. C语言 C++ C# Java Python Node.js PH ...
- 发布MeteoInfo 1.2.4
在JLaTeXMath库(http://forge.scilab.org/index.php/p/jlatexmath/)的支持下,实现了利用LaTeX语法显示特殊符号和数学公式的功能.需要在字符串首 ...
- 【Flutter 混合开发】与原生通信-BasicMessageChannel
Flutter 混合开发系列 包含如下: 嵌入原生View-Android 嵌入原生View-iOS 与原生通信-MethodChannel 与原生通信-BasicMessageChannel 与原生 ...
- STL: set和map的区别、联系、使用
set是一种关联式容器,其特性如下: set以RBTree作为底层容器 所得元素的只有key(键)没有value(值) 不允许出现键重复 所有的元素都会被自动排序 不能通过迭代器来改变set的值,因为 ...
- dubbo-config-spring自定义xml标签扩展
要实现自定义自定义标签扩展,需要有如下步骤(在spring中定义了两个接口NamespaceHandler.BeanDefinitionParser,用来实现扩展) 1.设计配置属性和JavaBean ...
- 【API进阶之路】研发需求突增3倍,测试团队集体闹离职
摘要:最近研发的需求量涨了3倍,开发团队拼命赶进度,可苦了测试团队. 本以为从一线研发转管理后会清闲一些,但是没想到,我还要充当救火队员的角色. 到了第四季度,各业务部门都在憋着劲儿冲业绩,毕竟这跟年 ...
- MapReduce工作原理详解
文章概览: 1.MapReduce简介 2.MapReduce有哪些角色?各自的作用是什么? 3.MapReduce程序执行流程 4.MapReduce工作原理 5.MapReduce中Shuffle ...