Sharing my 16ms C++ solution


  • 0
    T
    class Solution {
    public:
        int threeSumSmaller(vector<int>& nums, int target) {
            sort(nums.begin(), nums.end());
            int n = nums.size();
            int result = 0;
            int i, left, right;
            for(i=0; i<n-2; i++)
            {
                left = i+1;
                right = n-1;
                while(left<right)
                {
                    if(nums[i]+nums[left]+nums[right]<target)
                    {
                        result = result+(right-left);
                        left++;
                    }
                    else
                    {
                        right--;
                    }
                }
            }
            
            return result;
        }
    };

Log in to reply
 

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