C++ solution


  • 0
    K

    int threeSumClosest(vector<int>& nums, int target) {
    sort(nums.begin(),nums.end());
    vector<int> ts;
    for (int i = 0; i <nums.size(); ++i) {
    for (int j = i+1; j <nums.size() ; ++j) {
    for (int k = j+1; k <nums.size() ; ++k) {
    ts.push_back(nums[i]+nums[j]+nums[k]);
    }
    }
    }
    for (int l = 0; l <ts.size() ; ++l) {
    if(abs(ts[0]-target)>abs(ts[l]-target))
    ts[0]=ts[l];
    }
    return ts[0];
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.