```
public int triangleNumber(int[] nums) {
if(nums.length < 3) return 0;
Arrays.sort(nums);
int count = 0;
for(int i = 2; i < nums.length; i++){
int l = 0;
int r = i - 1;
while(l < r){
while(l < r && nums[l] + nums[r] <= nums[i]) l++;
if(l < r) count += r - l;
r--;
}
}
return count;
}
```