每次外层循环固定住一个数字, 然后剩下两个使用two pointer方法 每次初始化成头和尾 然后根据大小挪动一次
public class Solution {
public int threeSumSmaller(int[] nums, int target) {
if ( nums == null || nums.length < 3 )
return 0;
Arrays.sort ( nums );
int count = 0;
for ( int i = 0; i < nums.length - 2; i ++ ){
int p1 = i + 1;
int p2 = nums.length - 1;
while ( p1 < p2 ){
if ( nums [ p1 ] + nums [ p2 ] < target - nums[ i ] ){
count += p2 - p1;
p1 ++;
}
else{
p2 --;
}
}
}
return count;
}
}

本文介绍了一种解决三数之和小于目标值问题的算法。通过固定一个数字,利用双指针技术在剩余数组中查找符合条件的组合。文章详细展示了具体的实现代码。
112

被折叠的 条评论
为什么被折叠?



