259 3 Sum Smaller
Given an array of n integers nums and a target, find the number of index tripletsi, j, k
with0 <= i < j < k < n
that satisfy the conditionnums[i] + nums[j] + nums[k] < target
.
For example, givennums=[-2, 0, 1, 3]
, andtarget= 2.
Return 2. Because there are two triplets which sums are less than 2:
Follow up: Could you solve it inO(n2) runtime?
这题思路跟L2sum II很像,只是换成了3sum而已。只要注意判断left和right的移动条件跟+res的条件就ok了。
Last updated