-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
39 lines (36 loc) · 793 Bytes
/
Copy pathSolution.java
File metadata and controls
39 lines (36 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Arrays;
public class Solution
{
public int triangleNumber(int[] nums)
{
if (nums == null || nums.length < 3)
{
return 0;
}
int count = 0;
int n = nums.length;
Arrays.sort(nums);
for (int i = 2; i < n; i++)
{
int a = nums[i];
int l = 0;
int r = i - 1;
while (l < r)
{
int b = nums[l];
int c = nums[r];
int sum = b + c;
if (sum > a)
{
count += (r - l);
r--;
}
else
{
l++;
}
}
}
return count;
}
}