출처 : https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/
Given a 0-indexed integer array nums
of length n
and an integer target
, return the number of pairs (i, j)
where 0 <= i < j < n
and nums[i] + nums[j] < target
.
class Solution {
public int countPairs(List<Integer> nums, int target) {
int sum = 0;
for (int a = 0; a < nums.size() - 1; a++) {
for (int b = a + 1; b < nums.size(); b++) {
if (nums.get(a) + nums.get(b) < target) {
sum += 1;
}
}
}
return sum;
}
}