Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created December 21, 2018 06:14
Show Gist options
  • Save kanrourou/f9fb0323e7ded02826fc1421fd7cf892 to your computer and use it in GitHub Desktop.
Save kanrourou/f9fb0323e7ded02826fc1421fd7cf892 to your computer and use it in GitHub Desktop.
class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
int len = nums.size(), res = 0;
if (len < 3)return 0;
sort(nums.begin(), nums.end());
for (int i = 0; i + 2 < len; ++i)
{
int lo = i + 1, hi = len - 1;
while (lo < hi)
{
int sum = nums[i] + nums[lo] + nums[hi];
if (sum >= target)
--hi;
else
{
res += hi - lo;
++lo;
}
}
}
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment