```
public class Solution {
public int triangleNumber(int[] nums) {
int ret = 0;
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
int slow = i + 1;
int fast = i + 2;
while (fast < nums.length) {
if (slow == fast) {
fast++;
} else if (nums[slow] + nums[i] <= nums[fast]) {
slow++;
} else {
ret += fast - slow;
fast++;
}
}
}
return ret;
}
}
```