My simple c++ solution


  • 0
    K
    class Solution {
    public:
    int threeSumSmaller(vector<int>& nums, int target) {
        sort(nums.begin(), nums.end());
        int count=0;
        for(int i=0; i < nums.size(); i++){
            int j=i+1, k=nums.size()-1;
            while(j < k){
                int sum=nums[i] + nums[j] + nums[k];
                if(sum < target)count+= (k-j);
                
                if(sum < target)j++;
                else k--;
            }
        }
        return count;
    }
    

    };


Log in to reply
 

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