9 Line Clean and Short C++ O(n^2)


  • 0
    F
    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)
                for (int j = i + 1, k = (int)nums.size() - 1; j < k;)
                    if (nums[j] + nums[k] < target - nums[i])
                        count += k - 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.