标签:style blog color os io for ar div
Simply a variation of 3Sum:
class Solution { public: int threeSumClosest(vector<int> &num, int target) { int ret = 99999999999; std::sort(num.begin(), num.end()); for (int i0 = 0; i0 < num.size() - 2; i0++) { int i1 = i0 + 1, i2 = num.size() - 1; while (i1 < i2) { int curr = num[i0] + num[i1] + num[i2]; if (curr == target) return target; else if (curr < target) i1 ++; else if (curr > target) i2 --; if (abs(curr - target) < abs(ret - target)) ret = curr; } } return ret; } };
LeetCode "3Sum closest",布布扣,bubuko.com
标签:style blog color os io for ar div
原文地址:http://www.cnblogs.com/tonix/p/3918991.html