LeetCode——3Sum Closest
Question
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).
Solution
- 先排序,然后遍历每一个元素nums[i],并且在其之后找nums[j],nums[k],j<k,使得结果最接近。
sum = nums[i] + nums[j] + nums[k];
if sum > target k--;
if sum < target j++;
if sum == target return;
Code
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int minValue = nums[0] + nums[1] + nums[2];
int res = abs(minValue - target);
for (int i = 0; i < nums.size() - 2; 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) < res) {
res = abs(sum - target);
minValue = sum;
}
if (sum > target)
k--;
else if (sum < target)
j++;
else
return sum;
}
}
return minValue;
}
};
LeetCode——3Sum Closest的更多相关文章
- [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 (Two pointers)
题意 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 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][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 ...
- 3Sum Closest - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...
随机推荐
- Nginx无法启动,80端口被PID=4占用
在nginx启动后,error.log中总是显示 80 端口被占用. 通过netstat -ano发现,其被一个叫PID=4的系统服务占用. 网上大多数的方法是说通过regidit修改注册表的方式解决 ...
- Xshell 连接虚拟机特别慢 解决方案
由于各种原因,xshell连接虚拟机的rhel或者CentOS都几乎是龟速...... 今天专门查了一下解决方案: 原来是ssh的服务端在连接时会自动检测dns环境是否一致导致的,修改为不检测即可,操 ...
- Sublime Text 中文
1.打开Sublime Text 2.Ctrl+Shift+P,输入Package Control: Install Package回车 3.输入LocalizedMenu,回车 4.点击菜单help ...
- linux下dubbo调试 ---telnet命令
linux下启动dubbo服务端, 怎么调试? 方法有二: 1. 自己写简单消费者功能,进行各种情况测试.(这确实是有必要的) 2. 使用telnet直接连接上dubbo,使用命令调用,然后调试.(这 ...
- SAP Idoc 事务码
SALE Display ALE Customizing SM59 RFC Destinations (Display/Maintain) BD64 Maintenance of Distributi ...
- Linux常见系统命令与文件操作
一.Linux常见文件操作 (1)cd /: 在Linux 系统中斜杠“/”表示的是根目录. cd / ,即进入根目录. (2)cd ~命令是,进入用户在该系统的home目录,例如xz用户,则进入/r ...
- 字符串的partition函数
partition函数 str1='sdga2a34'aa=str1.partition('a') print(aa) """ ('sdg', 'a', '2a34') ...
- JQuery变量数字相加的研究
在 jquery中,一个变量和一个数字相加,期望得到1+2=3但是: eg: <input type="text" class="input_Num" i ...
- mysql锁机制之间隙锁(Next-Key锁)(五)
间隙锁(Next-Key锁) 当我们用范围条件而不是相等条件检索数据,并请求共享或排他锁时,InnoDB会给符合条件的已有数据记录的 索引项加锁:对于键值在条件范围内但并不存在的记录,叫做“间隙(GA ...
- SpringCloud Config Server中{application}等占位符使用场景设置默认拉去分支
Spring Cloud Config服务器支持一个Git仓库URL,其中包含{application}和{profile}(以及{label})的占位符. 1.各个占位符所代表的含义 applica ...