LeetCode -- 3Sum Closest
来源:互联网 发布:小米手机关闭移动数据 编辑:程序博客网 时间:2024/06/08 00:52
题目描述:
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).
本题与 Three Sum Zero类似,不同点在于,之前是寻找a+b+c=0的组合,现在是找最接近target的a+b+c的值。
思路:
依然是two pointer的思路。
1.对数组排序
2.使用a表示当前第i轮查找,b=i+1,表示第i轮的第1个元素,b∈[i+1,n),c=len-1,一开始指向最后一个元素。
3.当找到a+b+c=target,直接返回;如果a+b+c < target,b++;否则,c--。
4.每次取Math.Abs(a+b+c- target)与当前Math.Abs(min-target)的最小值。
5.最后返回min
实现代码:
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).
本题与 Three Sum Zero类似,不同点在于,之前是寻找a+b+c=0的组合,现在是找最接近target的a+b+c的值。
思路:
依然是two pointer的思路。
1.对数组排序
2.使用a表示当前第i轮查找,b=i+1,表示第i轮的第1个元素,b∈[i+1,n),c=len-1,一开始指向最后一个元素。
3.当找到a+b+c=target,直接返回;如果a+b+c < target,b++;否则,c--。
4.每次取Math.Abs(a+b+c- target)与当前Math.Abs(min-target)的最小值。
5.最后返回min
实现代码:
public class Solution { public int ThreeSumClosest(int[] nums, int target) { if(nums == null || nums.Length < 3){ return nums.Sum(); } var list = nums.OrderBy(x=>x).ToList(); var len = list.Count; int? min = null; for (var i = 0 ;i <= len - 3 ;i++) { var a = list[i]; var start = i+1; var end = len-1; while (start < end) { var b = list[start]; var c = list[end]; if(a+b+c == target){ min = target; break; } else if (a+b+c > target){ end --; } else{ start ++; } // update min if (min == null || Math.Abs(a+b+c - target) < Math.Abs(min.Value - target)) { min = a+b+c; } } } return min.Value; }}
0 0
- LeetCode: 3 Sum Closest
- leetcode - 3 sum closest
- leetcode 3Sum Closest
- LeetCode: 3Sum Closest
- [Leetcode]3Sum Closest
- Leetcode: 3SUM Closest
- LeetCode 3Sum Closest
- [Leetcode] 3Sum Closest
- LeetCode : 3Sum Closest
- LeetCode 3Sum Closest
- [LeetCode] 3Sum Closest
- 【leetcode】3Sum Closest
- [LeetCode]3Sum Closest
- LeetCode-3Sum Closest
- LeetCode:3Sum Closest
- LeetCode | 3Sum Closest
- 【leetcode】3Sum Closest
- Leetcode: 3Sum Closest
- 善用php-fpm的慢执行日志slow log分析PHP执行效率和程序BUG
- for_each调用类成员函数,mem_fun, mem_fun_ref用法
- map的详细用法
- hpuoj 1714: 感恩节KK专场——送给新生的礼物 (水题)
- Error:不能再成员函数"Xxx::xxxxxx"的类外部重新声明该函数
- LeetCode -- 3Sum Closest
- hpuoj 1715: 感恩节KK专场——2015年的第一场雪 (暴力)
- stm32 实现串口中断接收浮点型、整型数据
- java基础之正则及面向对象
- volley解惑
- 浅谈三层架构
- LeetCode -- 4Sum
- iOS中的开发模式
- 启动其他应用