-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathValidTriangleNumber.java
50 lines (48 loc) · 1.28 KB
/
ValidTriangleNumber.java
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
40
41
42
43
44
45
46
47
48
49
50
/*https://leetcode.com/problems/valid-triangle-number/*/
class Solution {
public int triangleNumber(int[] nums) {
if (nums.length <= 2) return 0;
Arrays.sort(nums);
int n = nums.length;
int i = 0, j = 0, result = 0;
for (i = 0; i < n-2; ++i)
{
for (j = i+1; j < n-1; ++j)
{
int index = -1, low = j+1, high = n-1, mid;
while (low <= high)
{
mid = low+(high-low)/2;
if (nums[i]+nums[j] > nums[mid])
{
index = mid;
low = mid+1;
}
else high = mid-1;
}
result += Math.max(0,index-j);
}
}
return result;
}
}
class Solution {
public int triangleNumber(int[] nums) {
Arrays.sort(nums);
int count = 0, n = nums.length;
for (int i = n-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;
}
}