Clean C++ 3 pointers

  • 0
        int threeSumClosest(vector<int>& nums, int target) {
            int closest=nums[0]+nums[1]+nums[2];
            for(int i=0;i<nums.size()-2;i++){
                int j=i+1,k=nums.size()-1;
                    int sum=nums[i]+nums[j]+nums[k];
                    if(sum==target) return sum;
                    if(abs(target-sum)<abs(target-closest)) closest=sum;
                    if(sum>target) k--;
                    else j++;
            return closest;

Log in to reply

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