611. Valid Triangle Number
Input: nums = [2,2,3,4]
Output: 3
Explanation: Valid combinations are:
2,3,4 (using the first 2)
2,3,4 (using the second 2)
2,2,3public int triangleNumber(int[] nums) {
if (nums == null || nums.length < 3) return 0;
int count = 0;
Arrays.sort(nums);
for (int i = nums.length - 1; i >= 2; i--) {
int l = 0, r = i - 1;
while (l < r) {
if (nums[l] + nums[r] > nums[i]) {
count += r - l;
r--;
} else {
l++;
}
}
}
return count;Last updated