8 lines short c++ solution


  • 0
    R
    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)
                    (nums[i]+nums[left]+nums[right]<target) ? result += right - left, left++ : right--;
            }
            return result;
        } 
    

    };


Log in to reply
 

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