C++ 12ms 7 lines


  • 1
    int threeSumSmaller(vector<int>& nums, int target) {
            sort(nums.begin(), nums.end());
            int cnt = 0;
            for (int k = 2; k < nums.size(); k++)
                for (int i = 0, j = k - 1, t = target - nums[k]; i < j;)
                    if (nums[i] + nums[j] < t) { cnt += j - i++; } 
                    else { j--;}
            return cnt;
    }
    

  • 0

    brilliant solution


Log in to reply
 

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