Triangle Count
[3,4,6]
[3,6,7]
[4,6,7][4(1),4(2),4(3)]
[4(1),4(2),4(4)]
[4(1),4(3),4(4)]
[4(2),4(3),4(4)]Last updated
[3,4,6]
[3,6,7]
[4,6,7][4(1),4(2),4(3)]
[4(1),4(2),4(4)]
[4(1),4(3),4(4)]
[4(2),4(3),4(4)]Last updated
public class Solution {
/*
* @param S: A list of integers
* @return: An integer
*/
public int triangleCount(int[] S) {
// write your code here
int n = S.length;
Arrays.sort(S);
int count = 0;
for(int i = 0; i < n; i ++){
int start = 0, end = i-1;
while(start < end){
if(S[start] + S[end] > S[i]) {
count+= end-start;//??????
end --;
}else{
start ++;
}
}
}
return count;
}
}