[LeetCode] 3Sum Closest
来源:互联网 发布:手机淘宝查看卖家信誉 编辑:程序博客网 时间:2024/06/05 20:21
Sort the array, for each a, search the sum of b,c from two ends to the middle, while updating the minimum distance between the target.
Similar solution could apply to 3Sum and 4Sum problems.
class Solution {public: int threeSumClosest(vector<int> &num, int target) { int min_dist = INT_MAX; int result; sort(num.begin(),num.end()); for(int i=0;i<num.size();i++) { int j=i+1,k=num.size()-1; while(j<k) { int tmp = num[i]+num[j]+num[k]; int dist = tmp>target?tmp-target:target-tmp; if(dist<min_dist) { min_dist = dist; result = tmp; } if(tmp>target) k--; else j++; } } return result; }};
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
- R3文件占坑大法
- C++按值返回、按址返回、按别名返回开销的区别
- Oracle RAC Cache Fusion 机制 详解 【偶像大神--dave】
- 腾讯大规模Hadoop集群实践
- 32位的SSDT表结构浅析
- [LeetCode] 3Sum Closest
- 构建高并发高可用的电商平台架构实践
- x86 IDT HOOK
- x86 SYSENTER HOOK
- x86 IRP HOOK
- Spring框架,使用ModelMap传值,jsp无法获取!
- x86 内核函数detour补丁
- x86 混合式钩子
- 结构体与共用体的妙用