[LintCode] Triangle Count
public int triangleCount(int S[]) { // write your code here int res = 0; if(S.length == 0 || S == null) return res; Arrays.sort(S); for(int i = S.length - 1; i > 0; i--) { int l = 0; int r = i - 1; while(l < r) { if(S[l] + S[r] > S[i]){ res += (r-l); r--; } else l++; } } return res; }
Leave A Comment