博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3sum-closest
阅读量:6244 次
发布时间:2019-06-22

本文共 3972 字,大约阅读时间需要 13 分钟。

https://leetcode.com/problems/3sum-closest/

// At first, my DP solution exceeds time limitation

// Then with the hint fo the discussion board,
// I have the final solution of impl2,
// which is very smart!

 

class Solution {    map
>, int> mp; int diff(int a, int b) { return a>=b ? a-b : b-a; } bool close(int a, int b) { if (b == 0) { // for init condition return true; } if (a < 0) { a *= -1; } if (b < 0) { b *= -1; } return a < b; } vector
nums; public: int threeSumClosest(vector
& n, int target) { nums = n; int vlen = nums.size(); if (vlen < 3) { return 0; } //return impl(target, vlen, 3); return impl2(target, vlen); } int impl2(const int &target, const int &end) { sort(nums.begin(), nums.end()); // can be 0, as if 0 returns asap int df = 0; int n; int p; int q; int ndf; for (int i = 0; i < end-2; i++) { n = nums[i]; p = i + 1; q = end - 1; while (p < q) { ndf = n + nums[p] + nums[q]; if (ndf == target) { return target; } else if (ndf > target) { q--; } else { p++; } if (close(ndf - target, df)) { df = ndf - target; } } } return target + df; } // below solution exceed time limit int impl(const int &target, const int &end, const int &sz) { if (sz == 0 || end == 0) { return 0; } pair
> pr = make_pair(target, make_pair(end, sz)); if (mp.find(pr) != mp.end()) { return mp[pr]; } int ret; int df; if (sz == 1) { ret = nums[0]; df = diff(ret, target); for (int i=1; i
>, int> mp; int diff(int a, int b) { return a>=b ? a-b : b-a; } bool close(int a, int b) { if (b == 0) { // for init condition return true; } if (a < 0) { a *= -1; } if (b < 0) { b *= -1; } return a < b; } vector
nums; public: int threeSumClosest(vector
& n, int target) { nums = n; int vlen = nums.size(); if (vlen < 3) { return 0; } //return impl(target, vlen, 3); return impl2(target, vlen); } int impl2(const int &target, const int &end) { sort(nums.begin(), nums.end()); // can be 0, as if 0 returns asap int df = 0; int n; int p; int q; int ndf; for (int i = 0; i < end-2; i++) { n = nums[i]; p = i + 1; q = end - 1; while (p < q) { ndf = n + nums[p] + nums[q]; if (ndf == target) { return target; } else if (ndf > target) { q--; } else { p++; } if (close(ndf - target, df)) { df = ndf - target; } } } return target + df; } // below solution exceed time limit int impl(const int &target, const int &end, const int &sz) { if (sz == 0 || end == 0) { return 0; } pair
> pr = make_pair(target, make_pair(end, sz)); if (mp.find(pr) != mp.end()) { return mp[pr]; } int ret; int df; if (sz == 1) { ret = nums[0]; df = diff(ret, target); for (int i=1; i
& nums, int target, int len) { if (len == 0) { return 0; } int vlen = nums.size(); if (vlen < len) { return 0; } int i= 0; int result = 0; if (vlen == len) { for (i=0; i
len) { nums.pop_back(); result = sumClosest(nums, target, len); nums.push_back(tail); } nums.pop_back(); int ret = sumClosest(nums, target-tail, len-1) + tail; nums.push_back(tail); int diff1 = target - result; diff1 = (diff1>0)?diff1:diff1*-1; int diff2 = target - ret; diff2 = (diff2>0)?diff2:diff2*-1; if(diff1 < diff2) { return result; } return ret; }

 

转载地址:http://adpia.baihongyu.com/

你可能感兴趣的文章
js常用的事件对象
查看>>
SharePoint 2013 禁用搜索服务
查看>>
[原]一个针对LVS的压力测试报告
查看>>
拥塞控制和流量控制
查看>>
[LeetCode] Sum Root to Leaf Numbers
查看>>
IO设计模式:Reactor和Proactor对比
查看>>
Qt Widgets——动作类与小部件菜单项
查看>>
ASP.NET MVC搭建项目后台UI框架—5、Demo演示Controller和View的交互
查看>>
[转]动态规划解最长公共子序列问题
查看>>
WorldWind源码剖析系列:影像存储类ImageStore、Nlt影像存储类NltImageStore和WMS影像存储类WmsImageStore...
查看>>
ORA-00600: 内部错误代码, 参数: [kqlnrc_1]
查看>>
Android Studio常用小技巧
查看>>
和为S的两个数字
查看>>
NPOI导出模板样式
查看>>
jsp请求由servlet响应的方式
查看>>
16 款最流行的 JavaScript 框架
查看>>
使用awrextr.sql导出awr原始数据
查看>>
分享一次失败的项目实践经验
查看>>
jedispool 连 redis
查看>>
PadLeft 补零
查看>>